Adrian Clark
Adrian Clark

Reputation: 12499

WPF Application fails on startup with TypeInitializationException

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?

Upvotes: 23

Views: 29965

Answers (11)

AviB
AviB

Reputation: 105

I was getting the same error. The suggestions mentioned above did not work for me. i was getting the following error after running

System.TypeInitializationException
  HResult=0x80131534
  Message=The type initializer for 'System.Windows.Application' threw an exception.
  Source=PresentationFramework
  StackTrace:
   at System.Windows.Application..ctor()
   at ShortBarDetectionSystem.App..ctor()
   at ShortBarDetectionSystem.App.Main()

Inner Exception 1:
TypeInitializationException: The type initializer for 'System.Windows.Navigation.BaseUriHelper' threw an exception.

Inner Exception 2:
TypeInitializationException: The type initializer for 'MS.Internal.TraceDependencyProperty' threw an exception.

Inner Exception 3:
ConfigurationErrorsException: Configuration system failed to initialize

Inner Exception 4:
ConfigurationErrorsException: Section or group name 'oracle.manageddataaccess.client' is already defined. Updates to this may only occur at the configuration level where it is defined. (C:\ShortBarDetectionSystem\code\framework\TypeInitializationException\ver0_1\ShortBarDetectionSystem\ShortBarDetectionSystem\bin\x64\Debug\GrateBarDefectDetectionSystem.exe.Config line 4)

I was getting an error in my exe.config file line 4. The .exe.config file was :

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="oracle.manageddataaccess.client" type="OracleInternal.Common.ODPMSectionHandler, Oracle.ManagedDataAccess, Version=4.122.21.1, Culture=neutral, PublicKeyToken=89b483f429c47342" />
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
  </startup>
  <system.data>
    <DbProviderFactories>
      <remove invariant="Oracle.ManagedDataAccess.Client" />
      <add name="ODP.NET, Managed Driver" invariant="Oracle.ManagedDataAccess.Client" description="Oracle Data Provider for .NET, Managed Driver" type="Oracle.ManagedDataAccess.Client.OracleClientFactory, Oracle.ManagedDataAccess, Version=4.122.21.1, Culture=neutral, PublicKeyToken=89b483f429c47342" />
    </DbProviderFactories>
  </system.data>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Text.Json" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

However after trial and error i figured out that deleting the configSections

<configSections>
    <section name="oracle.manageddataaccess.client" type="OracleInternal.Common.ODPMSectionHandler, Oracle.ManagedDataAccess, Version=4.122.21.1, Culture=neutral, PublicKeyToken=89b483f429c47342" />
  </configSections>

worked for me.

Upvotes: 0

Sely Lychee
Sely Lychee

Reputation: 312

For me I renamed my Application name and caused this error. I had a server and client app. the server app was not having this issue. so i checked App.config file of both server and client. I found

<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>

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

<startup> tag above <configSections> tag in client and server had the other way so I copy pasted startup tag down configSections tag and it worked. Like this.

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

<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>

Upvotes: 0

Jason Cheng
Jason Cheng

Reputation: 340

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.

Upvotes: 2

Denis Kirin
Denis Kirin

Reputation: 1

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

Upvotes: 0

lvmeijer
lvmeijer

Reputation: 1032

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.

Upvotes: 2

usefulBee
usefulBee

Reputation: 9702

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

Upvotes: 2

Lin Song Yang
Lin Song Yang

Reputation: 1944

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

Upvotes: 11

Umesh Bhavsar
Umesh Bhavsar

Reputation: 1

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

Upvotes: 0

Daniel Rose
Daniel Rose

Reputation:

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

Upvotes: 0

Adrian Clark
Adrian Clark

Reputation: 12499

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.

Upvotes: 37

ima
ima

Reputation: 8265

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.

Upvotes: 8

Related Questions