Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Application fails on startup with TypeInitializationException

Tags:

.net

wpf

prism

I have a simple WPF application which I am trying to start. I am following the Microsoft Patterns and Practices "Composite Application Guidance for WPF". I've followed their instructions however my WPF application fails immediately with a "TypeInitializationException".

The InnerException property reveals that "The type initializer for 'System.Windows.Navigation.BaseUriHelper' threw an exception."

Here is my app.xaml:

<Application x:Class="MyNamespace.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Application.Resources>         
    </Application.Resources>
</Application>

And here is my app.xaml.cs (exception thrown at "public App()"):

public partial class App : Application
{
    public App()
    {
        Bootstrapper bootStrapper = new Bootstrapper();
        bootStrapper.Run();
    }
}

I have set the "App" class as the startup object in the project.

What is going astray?

like image 241
Adrian Clark Avatar asked Sep 12 '08 07:09

Adrian Clark


3 Answers

Thanks @ima, your answer pointed me in the right direction. I was using an app.config file and it contained this:

<configuration>   <startup>     <supportedRuntime version="v2.0.50727" sku="Client"/>   </startup>   <configSections>     <section name="modules" type="Microsoft.Practices.Composite.Modularity.ModulesConfigurationSection, Microsoft.Practices.Composite"/>   </configSections>   <modules>     <module assemblyFile="Modules/MyNamespace.Modules.ModuleName.dll" moduleType="MyNamespace.Modules.ModuleName.ModuleClass" moduleName="Name"/>   </modules> </configuration> 

It seems the problem was the <startup> element because when I removed it the application ran fine. I was confused because Visual Studio 2008 added that when I checked the box to utilise the "Client Profile" available in 3.5 SP1.

After some mucking about checking and un-checking the box I ended up with a configuration file like this:

<configuration>   <configSections>     <section name="modules" type="Microsoft.Practices.Composite.Modularity.ModulesConfigurationSection, Microsoft.Practices.Composite"/>   </configSections>   <modules>     <module assemblyFile="Modules/MyNamespace.Modules.ModuleName.dll" moduleType="MyNamespace.Modules.ModuleName.ModuleClass" moduleName="Name"/>   </modules>   <startup>     <supportedRuntime version="v2.0.50727" sku="Client"/>   </startup> </configuration> 

Which works!

I'm not sure why the order of elements in the app.config is important - but it seems it is.

like image 79
Adrian Clark Avatar answered Sep 20 '22 12:09

Adrian Clark


Anything wrong in the App.config file may cause the error, such as a typo of * at the end of a line, eg ...</startup> has an additional "*" at the end of the line ...</startup>*.

like image 44
Lin Song Yang Avatar answered Sep 21 '22 12:09

Lin Song Yang


Do you use .config file? If so, check it for errors. Initialization errors of such sort are often triggered by invalid XML: if there are no errors in XAML, XML config is the first place to look.

like image 24
ima Avatar answered Sep 20 '22 12:09

ima