WPF Application fails on startup with TypeInitializationException

前端 未结 10 815
Happy的楠姐
Happy的楠姐 2020-12-17 09:05

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

相关标签:
10条回答
  • 2020-12-17 09:33

    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.

    0 讨论(0)
  • 2020-12-17 09:33

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

    0 讨论(0)
  • 2020-12-17 09:35

    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.

    0 讨论(0)
  • 2020-12-17 09:36

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

    0 讨论(0)
  • 2020-12-17 09:37

    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.

    0 讨论(0)
  • 2020-12-17 09:39

    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>
    
    0 讨论(0)
提交回复
热议问题