11

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?

CJBS
  • 15,147
  • 6
  • 86
  • 135
wrightjm
  • 113
  • 1
  • 1
  • 4
  • Can you supply us App.Main() code? – Dragomir Răzvan Feb 27 '14 at 23:49
  • You might check all your references (including .net framework) and make sure you aren't still referencing any 3.5 framework assemblies. – JMarsch Feb 28 '14 at 00:10
  • @DragomirRăzvan Here is the code for app.xaml.cs. I'm not quite sure why the original author didn't add the standard initialization code, but I've tried adding it and still get the same error. `using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Linq; using System.Windows; namespace MiniMon { /// /// Interaction logic for App.xaml /// public partial class App : Application { } }` – wrightjm Feb 28 '14 at 01:21
  • Sorry, I'm new to stackoverflow. – wrightjm Feb 28 '14 at 01:23
  • @JMarsch I checked and there are no 3.5 framework assemblies being referenced. I also tried removing and re-adding the references for the WPF assemblies. That didn't help. – wrightjm Feb 28 '14 at 01:25
  • 1
    According to [the documentation for the `System.TypeInitializationException`](http://msdn.microsoft.com/library/System.TypeInitializationException.aspx "TypeInitializationException Class"): "When a class initializer fails to initialize a type, a TypeInitializationException is created and passed a reference to the exception thrown by the type's class initializer. The InnerException property of TypeInitializationException holds the underlying exception." Have you looked at the inner exception? – Paulo Morgado Feb 28 '14 at 23:21
  • @PauloMorgado - I couldn't get Visual Studio 2013 to give me the inner exception. It kept saying that it would have to load symbols because the error occurred within a dll, but even after those loaded I still couldn't get any more information. – wrightjm Apr 15 '14 at 16:22
  • Looks like the issue is caused by something being wrong with the App.Config; In your case it's probably because it says it's targeting the wrong version of .NET, but it could really be anything. Make sure to check the `InnerException` trail and/or to examine the file by hand to see what's up. – BrainSlugs83 Jul 19 '16 at 02:05

8 Answers8

30

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>

Reza M.A
  • 1,197
  • 1
  • 16
  • 33
  • Mess in App.config was preventing me from launching the WPF app too. – mwilczynski Jan 28 '15 at 15:47
  • I had the same exact error as OP, and it ended up being caused by a syntax error in App.Config. Seems like the exception that's thrown is definitely caused by something being wrong with App.Config. – BrainSlugs83 Jul 19 '16 at 02:03
9

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.

Guruprasad J Rao
  • 29,410
  • 14
  • 101
  • 200
6

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

usefulBee
  • 9,250
  • 10
  • 51
  • 89
3

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.

Shaaak
  • 585
  • 3
  • 9
  • As you mentioned @shahar, this certainly wasn't an idea solution, but it's the one I ended up having to go with. The application ran without any code changes once I copied everything over to the new .NET 4.5 project. – wrightjm Apr 15 '14 at 16:18
1

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

0

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

mathielo
  • 6,725
  • 7
  • 50
  • 63
0

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

Mbithy Mbithy
  • 138
  • 13
0

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.

Lee Song
  • 601
  • 1
  • 5
  • 17