Reputation: 24721
My Windows Forms application was working earlier, however suddenly it stopped working. I am getting following exception:
With exception details as follows:
System.TypeInitializationException was unhandled
Message: An unhandled exception of type 'System.TypeInitializationException' occurred in mscorlib.dll
Additional information: The type initializer for 'NotificationTester.Program' threw an exception.
When I click OK, the VS windows then shows following:
The solution was working fine earlier. I don't get whats going wrong.
Upvotes: 35
Views: 92516
Reputation: 27852
After trying all of the answers listed here, I have a new one:
The type initializer for 'MyLibrary' threw an exception.
If you see something like the below in the InnerException(s)..........
"Could not load file or assembly 'log4net, Version=1.2.13.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a' or one of its dependencies. The system cannot find the file specified.":"log4net, Version=1.2.13.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a"
In my case, my directory had the correct version dll. (log4net.dll in this case).
And then ... the issue was "discovered". Gaaaaa!! Assembly redirects in the app.config.
:(
Since I had the correct version of the dll file, I removed all my redirects. Your situation may be different.
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="log4net" publicKeyToken="b32731d11ce58905" />
<codeBase version="1.2.9.0" href="log4netv1.2.9.0\log4net.dll" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="log4net" publicKeyToken="1b44e1d426115821" />
<codeBase version="1.2.10.0" href="log4netv1.2.10.0\log4net.dll" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="log4net" publicKeyToken="669e0ddf0bb1aa2a" />
<codeBase version="1.2.13.0" href="log4netv1.2.13.0\log4net.dll" />
</dependentAssembly>
</assemblyBinding>
</runtime>
Upvotes: 1
Reputation: 3059
Another possible reason:
Let's say you have such a property:
static Dictionary<string, ComplexObject> Objects = new Dictionary<string, ComplexObject>()
{
{"key1", new ... } // again complex initializer object
}
In case you use the Objects
property inside the initializer object, such an exception will raise.
It makes sense: There is no way to initialize a property using its own value which is not yet been initialized!
Upvotes: 0
Reputation: 320
The reason is the app.config/Web.config has been changed in some sections.
Upvotes: 0
Reputation: 839
In my case it was a very silly thing. I had accidentally typed something like this
<configuration>
<appSettings>
<add key="someKey" value="SomeValue" />sss
<add key="someKey1" value="SomeValue1" />
</appSettings>
</configuration>
sss - Caused the error for me. Any random character entry.
Upvotes: 2
Reputation: 87
I've the same problem: runs program in MS VS 2015 and triggers an exception message:
System.TypeInitializationException was unhandled
Message: An unhandled exception of type 'System.TypeInitializationException' occurred in mscorlib.dll
Additional information: Der Typeninitialisierer für "<Module>" hat eine Ausnahme verursacht.
The question where source this exception? So, I started the EXE file and displayed the exception message dialog. Answer on "debug mode" with MS VS so become more information displayed about exception.
System.IO.FileNotFoundException was unhandled
Message: An unhandled exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll
Additional information: Die Datei oder Assembly "SIC, Version=19.2.6.3, Culture=neutral, PublicKeyToken=**************" oder eine Abhängigkeit davon wurde nicht gefunden. Das System kann die angegebene Datei nicht finden.
I go to this reference and analyse properties Property: "Copy Local" is on false - because before was a installed into GAC Solution: property "Copy Local" set on true and builds new assembly and works :-)
Maybe this information helpful! So have a good day.
Upvotes: 0
Reputation: 51
This issue for me was caused by a rogue user.config file that was created in the AppData\Local[Manufacturer][Product Name] directory. I'm not sure how it got there, but seems to be created every now and then.
Upvotes: 0
Reputation: 1561
So, If you didn't get an innerException message like me, you can set a breakpoint on some of the static variables like strings that you may have declared. Then you can debug forwards from there with F10.
Upvotes: 1
Reputation: 17508
Make sure you are not missing any dependency DLLs. In my case, a DLL I was referencing had a dependency on another DLL.
If this is what happened, look at the "Inner Exception" property and then you will see a better error message. In my case, it said "Cannot find xxx.dll"
Upvotes: 1
Reputation: 3272
In my case the reason was, that <configSections>
wasn't first one in config
file.
Only one
<configSections>
element allowed per config file and if present must be the first child of the root<configuration>
element.
Move the configSections
element to the top in your config file.
Hope it helps to someone.
Upvotes: 3
Reputation: 2186
This is one weird issue I had to deal with for the last 2 hours. I solved it by removing the static from the lists I created.
private static readonly List<Person> someList = GlobalConfiguration.Connection.PopulateList();
with this one:
private readonly List<Person> someList = GlobalConfiguration.Connection.PopulateList();
Hope it helps and you don't have to spend two hours to find out the bug...
Upvotes: 1
Reputation: 5731
A possible reason: init a static dictionary with duplicated keys.
Upvotes: 9
Reputation: 21
I've got the same error with platform target set as any CPU and prefer 32-bit checked, unchecking that last one solved my problem.
Upvotes: 1
Reputation: 71
I got the same error message and for my case the reason is my main program is set to build as 32 bit console app and I added a private variable logger which access a 64bit dll.
public class Program
{
public static readonly Logger logger = new Logger(typeof(Program));
After I changed the main program to be build as 64bit, the issue is fixed.
Upvotes: 7
Reputation: 1062600
So: either one of the field-initializers, or the static constructor, for Program
- is failing. Find out why. Note: the InnerException
has the actual exception that was raised, but basicaly: just debug the field initializers and static constructor. So look inside the Program
class for either:
static SomeType someField = /* some non-trivial expression or method call */
or:
static Program() {
// stuff
}
Upvotes: 49