13

I am getting an error on the very first line of code in the App.cs file (which is creating a readonly variable). The error I am getting is:

A first chance exception of type 'System.TypeInitializationException' occurred in PresentationFramework.dll An unhandled exception of type 'System.TypeInitializationException' occurred in PresentationFramework.dll Additional information: The type initializer for 'System.Windows.Application' threw an exception.

This is the message popup I get in VS:

An unhandled exception of type 'System.TypeInitializationException' occurred in PresentationFramework.dll

Additional information: The type initializer for 'System.Windows.Application' threw an exception.

The only change I have made is adding this to my app.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1"/>
  </startup>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>
  <log4net>
    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" >
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline" />
      </layout>
    </appender>
    <root>
      <level value="INFO" />
      <appender-ref ref="ConsoleAppender" />
    </root>
  </log4net>
</configuration>
Cheetah
  • 13,785
  • 31
  • 106
  • 190
  • Have you added a reference to `log4net` to the same project that contains this `app.config`? – Chris Ballard Mar 23 '14 at 20:10
  • 1
    If yes, then can you copy and paste the full stack trace for this exception - that will help a lot – Chris Ballard Mar 23 '14 at 20:11
  • @Chris - yes the reference is there. See edit for full exception details. – Cheetah Mar 23 '14 at 20:15
  • @Prix - if I remove the quoted `app.config` then its fine. – Cheetah Mar 23 '14 at 20:22
  • @Ben post the complete app.config as yours look incomplete – Prix Mar 23 '14 at 20:23
  • @Prix - sorry I didn't think the rest was relevant as it was just the quoted bits that were causing the issues....however, I have now edited with the full config. – Cheetah Mar 23 '14 at 20:25
  • 3
    @Ben make `` the first element and see if that works. In other words put it before ``. – Prix Mar 23 '14 at 20:27
  • @Prix - that is ridiculous - but works - Thanks. – Cheetah Mar 23 '14 at 20:33
  • Next time do a little more research before posting the question, duplicate of [Why would adding an \*\*appSettings\*\* section to App.config cause an error in WPF application?](http://stackoverflow.com/questions/1223142/why-would-adding-an-appsettings-section-to-app-config-cause-an-error-in-wpf) – Prix Mar 23 '14 at 20:36
  • Something is corrupted in your app.config (maybe you copy/paste some tags?) – Masciuniria Oct 03 '18 at 11:00

2 Answers2

22

The issue was that I had the <startup> xml node in the app.config file at the start rather than the end of the file. It needs to be the last thing in the app.config file.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  // lots of other stuff here...
  <startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1"/>
  </startup>
</configuration>
Cheetah
  • 13,785
  • 31
  • 106
  • 190
0

I had same error. I found that <configSections> has to be the first element.

Mario M.
  • 416
  • 2
  • 10