Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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>*.

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.

Tracking the InnerExceptions deep down , you might find the following error:

"Only one <configSections> element allowed per config file and if present must be the first child of the root <configuration> element"

This order change happened after Visual Studio EntityFramework Wizard added the connectionStrings element to the top

If you only see the TypeInitializationException with no reason or no details on what's wrong, then disable Just My Code in the Visual Studio options.

For me, I had copied app settings over from another application into my app.config into a new section called "userSettings". However, there needs to be a "configSections" also added to the app.config which defines "userSettings". I deleted the userSettings section then edited the app settings and saved it. VS automatically creates the correct "userSettings" and "configSections" for you if they are absent.

You have two sections named "modules". Place both module definitions in one section named "modules".

I ran into a similar situation. After searching for a week time, I found the resolution and it really worked for me. It solved 2-3 problems arising due to same problem.

Follow these steps: Check the WPF key (absence) in registry: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.0\Setup\Windows Presentation Foundation My problem was due to the absence of above mentioned key in registry.

You can modify and use following details in your registry: (Actually, you can save in file and import in registry)

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.0\Setup\Windows Presentation Foundation] @="WPF v3.0.6920.1453" "Version"="3.0.6920.1453" "WPFReferenceAssembliesPathx86"="C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\" "WPFCommonAssembliesPathx86"="C:\Windows\System32\" "InstallRoot"="C:\Windows\Microsoft.NET\Framework\v3.0\WPF\" "InstallSuccess"=dword:00000001 "ProductVersion"="3.0.6920.1453" "WPFNonReferenceAssembliesPathx86"="C:\Windows\Microsoft.NET\Framework\v3.0\WPF\"

I am sure it will work.

all the best.

Regards,

Umesh

In my case this is need to be added:

<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />

Section at App.config (VS 2015 .NET 4.5.2)

Open any WPF project what builded before, check build, if OK - check and compare App.config's at both projects

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top