Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method not found: BaseJsonMediaTypeFormatter.get_SerializerSettings() when using Newtonsoft.Json 10.0.2

Tags:

json.net

I am creating a method that returns an HttpResponseMessage. I need to use a custom JSON Formatter, as shown below:

    var jsonFormatter = new JsonMediaTypeFormatter();
    jsonFormatter.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
    jsonFormatter.SerializerSettings.Formatting = Formatting.Indented;
    jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
    IsoDateTimeConverter dateConverter = new IsoDateTimeConverter
    {
        DateTimeFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss.fff'Z'"
    };
    jsonFormatter.SerializerSettings.Converters.Add(dateConverter);

When I use Newtonsoft.Json version="10.0.2" or "10.0.1" and targetFramework="net461", I get the exception below:

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

If I downgrade to Newtonsoft.Json version="9.0.1", then it works fine. However, we would like to get some of the async support of version 10.

Is this a know bug? Is there a work around?

like image 363
Paco de la Cruz Avatar asked Oct 15 '25 04:10

Paco de la Cruz


1 Answers

I had this issue when an old web.config on a server had a binding redirect to v8.0.0 even though v10.0.0 was present in the bin folder.

I changed:

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

to:

<dependentAssembly>
  <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
  <bindingRedirect oldVersion="0.0.0.0-10.0.0.0" newVersion="10.0.0.0" />
</dependentAssembly>

and the issue was resolved.

like image 179
Martin Owen Avatar answered Oct 18 '25 04:10

Martin Owen