Search code examples
c#visual-studio-2013json.net.net-4.5

Newtonsoft.Json issue: could not load file or assembly exception from source System.Net.Http.Formatting


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>

Solution

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