My Windows Forms application was working earlier, however suddenly it stopped working. I am getting following exception:
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"
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 found. Assembly redirects in the app.config.
:(
Since I had the correct version, 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>
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...
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.
Another possible reason: the app.config has duplicate sections.
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
}