While I was working on some code today and found that some NuGet packages where out of date. So I went to update tab in Manage NuGet packages window and updated all of them.
MySql.Data package (not important which package, just an example) was also updated in that from version
6.7.8 to current / latest (
6.9.6). I tried to run the application to test the updates made in referring packages.
I encountered following error:
TypeInitializationException was unhandled
An unhandled exception of type 'System.TypeInitializationException' occurred in Unknown Module.
Additional information: The type initializer for '[Application Name]' threw an exception.
Looking at this exception, it doesn't seems to be obvious what is going wrong here. There isn't even option for "View details". Many of you may get stuck on this and spent hours before you figure out that the
bindingRedirect are creating this funnies.
If you go through your
web.config file you will see a section similar to following:
<runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="MySql.Data" publicKeyToken="c5687fc88969c44d" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-184.108.40.206" newVersion="220.127.116.11" /> </dependentAssembly> </assemblyBinding> </runtime>
This is used for redirecting assembly versions.
You can redirect compile-time binding references to .NET Framework assemblies, third-party assemblies, or your own app's assemblies. You can redirect your app to use a different version of an assembly in a number of ways: through publisher policy, through an app configuration file; or through the machine configuration file. This article discusses how assembly binding works in the .NET Framework and how it can be configured.
This was inserted by the NuGet package -
MySql.ConnectorNET.Data.*.*.*.* and somehow was not updated when I updated NuGet packages. After I updated this piece of code in configuration file as follows, application started successfully and I didn't encountered above exception anymore.
<runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="MySql.Data" publicKeyToken="c5687fc88969c44d" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-18.104.22.168" newVersion="22.214.171.124" /> </dependentAssembly> </assemblyBinding> </runtime>
Look for all the
assemblyBinding nodes in your configuration file(s) and check the version number of your currently used dll (Right click > Properties > Details > FileVersion attribute) and updated the version number in
bindingRedirect node appropriately.
Hope this helps.
Happy Coding !!