Dr.Doog
Dr.Doog

Reputation: 197

System.TypeInitializationException is thrown when Prism 6 WPF application is starting

I develop Prism 6 view-switching modular WPF MVVM application with Unity in MSVS 2015 Professional (russified). System.TypeInitializationException with message "The type initializer for 'System.Windows.Application' threw an exception" is thrown when my application is starting. This exception began to throw after I had added modules registration in App.config file. Below is the structure of my application solution:

enter image description here

As you can see the solution has main project "FlowmeterConfiguration" and two Prism Module projects: Authorization and Calibration. The names of Prism Module projects deffer from the names of their module classes. Authorization project has AuthorizationModule class:

namespace Authorization
{
    [Module(ModuleName = "AuthorizationModule", OnDemand = false)]
    public class AuthorizationModule : IModule
    {
       . . . . .
    }
}

and Calibration project has CalibrationModule class:

namespace Calibration
{
    [Module(ModuleName = "CalibrationModule", OnDemand = false)]
    public class CalibrationModule : IModule
    {
       . . . . .
    }
}

Below is App.config file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
    </startup>
    <modules>
        <module assemblyFile="Authorization.dll" moduleType="Authorization.AuthorizationModule, Authorization, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" moduleName="AuthorizationModule" startupLoaded="true" />
        <module assemblyFile="Calibration.dll" moduleType="Calibration.CalibrationModule, Calibration, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" moduleName="CalibrationModule" startupLoaded="true" />
    </modules>
</configuration>

Below is Bootstrepper class:

namespace FlowmeterConfigurator
{
    class Bootstrapper : UnityBootstrapper
    {
        protected override DependencyObject CreateShell()
        {
            return Container.Resolve<MainWindow>();
        }

        protected override void InitializeShell()
        {
            Application.Current.MainWindow.Show();
        }

        protected override IModuleCatalog CreateModuleCatalog()
        {
            return new ConfigurationModuleCatalog();
        }
    }
}

If I delete modules registration from App.config and CreateModuleCatalog method from bootstrapper, the exception is not thown and the application starts successfully. If I try to register modules through code (in Bootstrepper) then module types: Authorization and Calibration is not recognized and are marked as error (Authorization.AuthrizationModule and Calibration.CalibrationModule are not recognized too in this case). But I must register my modules! What I'm doing wrong? Please help me.

Upvotes: 0

Views: 596

Answers (2)

gabba
gabba

Reputation: 2880

Some exception thrown in static constructor or initializers. Try to look at inner exceptions. This is the similar problem: TypeInitializationException thrown for Program class

The visual studio with enabled debugger must show you exception window, just ckick to "View Detail..." link to see inner exception and stack. Probably it help you.

Upvotes: 0

user5420778
user5420778

Reputation:

Your App.config is missing the sections definition in the configsSection:

https://github.com/PrismLibrary/Prism/blob/master/Documentation/WPF/30-ModularApplicationDevelopment.md#registering-modules-using-a-configuration-file

If I try to register modules through code (in Bootstrepper) then module types: Authorization and Calibration is not recognized and are marked as error

If you want to reference your modules in code, you have to add a reference to the module projects first. Since you were trying to use the App.config, your Shell application does not have a reference to those projects.

Upvotes: 2

Related Questions