Reputation: 10638
I have a Windows service done in .NET 4.5. It is referencing a DLL located in another Visual Studio Solution, let's say, myCustomDLL. myCustomDLL has a reference to Newtonsoft.Json DLL version 11.0.1 and also a reference to System.Net.Http.Formatting version 5.2.6.0.
When I debug my window service from Visual Studio and call a function whitin myCustomDLL I get the error:
Could not load fiile or assembly 'Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies.
Source: System.Net.Http.Formatting
StackTrace:
at System.Net.Http.Formatting.JsonMediaTypeFormatter..ctor()
at System.Net.Http.Formatting.MediaTypeFormatterCollection.CreateDefaultFormatters()
at System.Net.Http.Formatting.MediaTypeFormatterCollection..ctor()
at System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent content)
at My.Namespace.MyAPI.Send(TaskData data) en d:\MyProjects\Dev\My.Custom.Namespace\myApi.cs:line 80
I haven't any Newtonsoft.json DLL version 4.5.0.0 installed on myCustomDLL.
Also my windows sercice (done in vb.net) has a refrence added pointing to Newtonsoft.Json version 11.0.1.
SOLUTION: Finally I have done what @Richard suggested in the comments and nilsK answered. Windows service had added the reference to the correct Newtonsoft.Json (11.0.1) but assemblyBinding lines were missed in the windows service configuration file (app.config) so I added them and it worked:
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-11.0.0.0" newVersion="11.0.0.0" />
</dependentAssembly>
</assemblyBinding>
Upvotes: 0
Views: 1174
Reputation: 121
If System.IO.FileLoadException
continues to be thrown even after binding redirects are added to Web.config, it is possible that there is a syntax error in the binding runtime/assemblyBinding
config block.
For example, due to a bad merge, I had two <bindingRedirect/>
blocks inside the same <dependentAssembly/>
block. This caused all <bindingRedirect/>
to not take effect, causing dlls to not be found during runtime. I can imagine other subtle syntax error will have the same side effect as well.
<runtime>
<assemblyBinding>
...
<!-- Problematic dependentAssembly line -->
<dependentAssembly>
<assemblyIdentity name="PackageA" publicKeyToken="token" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-1.0.0.0" newVersion="1.0.0.0" />
<!-- Example of syntax error -->
<!-- Two sets of bindingRedirects should NOT be placed in the same dependentAssembly -->
<assemblyIdentity name="PackageB" publicKeyToken="token" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
<dependentAssembly/>
</assemblyBinding>
</runtime>
Upvotes: 0
Reputation: 4351
Writing here, because better formatting ... i just want to add to @Richard's comment.
In your app.config, you will have some lines as shown below (may look a little different):
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.5.0.0" newVersion="4.5.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
Change that version number to the version you need i.e. '11.0.1' (or '12.0.3', current stable release).
Upvotes: 2