Method not found: ‘Void Newtonsoft.Json.Serialization.DefaultContractResolver.set_IgnoreSerializableAttribute(Boolean)’

json.netmsbuildsystem.net

I have a problem regarding NewtonSoft.

I have a solution with 3 projects for example. Project A has reference points to Project B and Project C, Project B also has reference points to Project C. Both B and C has NewtonSoft assembly. Project C has function to get JsonMediaTypeFormatter:

new JsonMediaTypeFormatter()  

the function is called by all three project. Both Project B and C can call the function without any problem. But when project A calls the function, it throws error:

Method not found: 'Void Newtonsoft.Json.Serialization.DefaultContractResolver.set_IgnoreSerializableAttribute(Boolean)' 

Something I notice that even project A does not have NewtonSoft reference, when project is built, the Newtonsoft.json.dll is copied to its bin\Debug folder. I guess it is because of Newtonsoft assembly is set to true for Copy Local optoin in both project B and C. If I manually delete this dll from Project A's bin\Debug folder, problem solved.

My question is why project A can hit the exception, and is there any solution except manually delete Newtonsoft dll from project A's bin\Debug folder? Set Copy Local to false is not a option because it will prevent dll deploying to their own bin folder for project B and C too.

Any suggestion would be appreciated.

Update

Here is my code snippet

using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
using System.Net.Http.Formatting;

var jsonSerializerSettings = new JsonSerializerSettings
{
    Formatting = Formatting.Indented,
    ContractResolver = new CamelCasePropertyNamesContractResolver()
};

var dateConverter = new IsoDateTimeConverter
{
    DateTimeFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"
};

jsonSerializerSettings.Converters.Add(dateConverter);

var formatter = new JsonMediaTypeFormatter
{
    //SerializerSettings = jsonSerializerSettings
};

If I comment out SerializerSettings, it works fine.

If I uncomment this line, application will return such issue.

If I just pass in blank setting to it

var formatter = new JsonMediaTypeFormatter
{
    //SerializerSettings = new JsonSerializerSettings{}
};

I got error:

Method not found: 'Void System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.set_SerializerSettings(Newtonsoft.Json.JsonSerializerSettings)'

I think it may be related to different System.Net.Http.Formatting references inside projects, but I checked reference setting, they are all point to file

packages\Microsoft.AspNet.WebApi.Client.5.2.3\lib\net45\System.Net.Http.Formatting.dll

This code resides in Project C. It only failed when called by Project A, project B and C can call it without any problem. Once I remove NewtonSoft.Json.dll from Project A bin\Debug folder, it works in project A calls too.

Do you know what happened? and how can I check if there still have different reference version conflicit in project?

Thanks,

Best Answer

Here I finally got problem solved.

The reaon for this issue is that in project A, there is a reference library points to System.Net.Http.Formatting (5.2.3.0), which depends on Newtonsoft Joson version 6.0.0.0. You can check this on Assembly Explorer. I updated Newtonsoft to 8.0.0.0 recently and project B and C are referencing this version. When project is built, the Newtonsoft 8.0 has been copy to project A's output folder based on prject B and C (Copy Local is set to true). when application is running, it will throw error

System.IO.FileLoadException : Could not load file or assembly 'Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

or in my debug mode, it throws

{"The 'CreateJsonSerializer' method threw an exception when attempting to create a JSON serializer."}

Solution:

Add app.config file in project A, including setting below, to redirect system assembly binding to correct NewtonSoft version

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-8.0.0.0" newVersion="8.0.0.0" />
  </dependentAssembly>
</assemblyBinding>

The reason project B can call project C successfully is that it has this redirect on its web.config file.

Thanks,

Related Topic