wrightjm
wrightjm

Reputation: 113

WPF Type initialization Exception in C#

I have someone else's WPF-based .NET 3.5 app that I'm attempting to update to .NET 4.5. The code ran fine under .NET 3.5, and I'm running Visual Studio 2013 Express on Windows 7. The update seemed to go well and the code compiles fine, but when I try to run the app I get the following exception.

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

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

Here are the last few steps in the stacktrace.

PresentationFramework.dll!System.Windows.Windows.Application()
MiniMon.exe!MiniMon.App.App()
MiniMon.exe!MiniMon.App.Main()

Here's the app.xaml file.

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

I can download a sample WPF app (WPFCalculator) and it runs fine, so I tried stripping the one I'm updating down to just what was in the sample app. I also tried adding a breakpoint at the entry point of the code in app.xaml.cs, but the exception is thrown even before that code is executed. As a last resort I tried running the app on Windows 8, but got the same error.

How can this problem be resolved?

Upvotes: 11

Views: 20537

Answers (8)

Reza M.A
Reza M.A

Reputation: 1207

I solve this problem by moving startup section in app.config to the last part before </configuration>, startup section must be the last part in app.config, like this:

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

Upvotes: 30

Lee Song
Lee Song

Reputation: 621

I replaced the depreciated Configuration library and also removed entity framework so I can't track what's wrong at all, but rebuilding helped me. Solution to Rebuild your App.Config file.

Upvotes: 0

Akshay Kumar Sharma
Akshay Kumar Sharma

Reputation: 271

<connectionStrings> tag should come after <configSections> and <startup> section. This made my code work.

Upvotes: 1

Mbithy Mbithy
Mbithy Mbithy

Reputation: 163

Check app.config file make sure that you don't have configurations to lib(s) that you no longer use or you have removed from the project

Upvotes: 0

Guruprasad J Rao
Guruprasad J Rao

Reputation: 29683

Late to the party, but found this reason in my case. I had added a key-value pair with appSettings in app.config as in

<appSettings>
    <add key="EncryKey" value="MyKey"/>
</appSettings>

before <configSections>. Upon digging in the internet, I came to know that, <configSections> had to be at the top of the root, soon after <configuration> and rest of the orders are irrelevant. Moving appSettings below <configSections> helped me fix this issue.

Upvotes: 9

David_Shervin
David_Shervin

Reputation: 23

Check the spellings of connectionStrings,connectionString="" if used! 'S' should be capital ..XML is case sensitive :)

Upvotes: 0

usefulBee
usefulBee

Reputation: 9692

Digging Deeper into the Exception Details down to the last InnerException, and I found this:

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

Move configSections to the beginning

Upvotes: 6

Shaaak
Shaaak

Reputation: 585

One (not very educational) workaround would be to start a new 4.5 project, and copy-paste the relevant pieces of code from the old one.

Upvotes: 3

Related Questions