0

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>
3
  • 2
    Quick answer, the config for your Windows Service project probably needs an assemblyBinding entry to ensure the right version of Newtonsoft.Json is loaded. Commented Jul 29, 2020 at 10:42
  • @Richard Sorry, I forgot to say that my windows service has a reference added to Newtonsoft.Json version 11.0.1. I am going to update post. Commented Jul 29, 2020 at 10:49
  • @Richard Thanks it worked! Commented Jul 29, 2020 at 19:35

2 Answers 2

2

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).

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, Finally I have done what @Richard suggested and it works ok. I have addded an assemblyBinding. It wasn't in the config file for my windows service. It only had added the reference to Newtonsoft.Json. AssemblyBinding in windows service config file was missed.
0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.