Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Unable to cast object of type 'System.Net.Http.Formatting.JsonContractResolver' to type 'Newtonsoft.Json.Serialization.DefaultContractResolver'."

Tags:

c#

json.net

We have a WEB API project that recently was moved to a new server. I'm running my project after making some additions to its' payload, but it suddenly throws the following error:

Unable to cast object of type 'System.Net.Http.Formatting.JsonContractResolver' to type 'Newtonsoft.Json.Serialization.DefaultContractResolver'.

The offending line of code is in global.asax:

  protected void Application_Start() {
        GlobalConfiguration.Configure(WebApiConfig.Register);

        var serializerSettings =
         GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings;
        var contractResolver =
           (DefaultContractResolver)serializerSettings.ContractResolver;
           contractResolver.IgnoreSerializableAttribute = true;
     }

I believe this code was added because the default output of the API was XML, and we need it to be JSON instead.

Highlighting (DefaultContractResolver) brings up a tooltip indicating it references NewtonSoft.JSon.Serialization.DefaultContractResolver. Highlighting serializersettings.ContractResolver references IContractResolver JSonSerializerSettings.ContractResolver.

The code has been on this machine for some time, and the only thing I can think I changed was installing a newer version of .NET.

What could cause this line of code to suddenly throw an error? And how can I resolve it?

Thanks!

Edit: As per request in the comments, my serialization code consists of something like the following:

json += "{\"employeename\": \"" + Convert.ToString(reader["Employee"])
+ "\"},";

return JsonConvert.DeserializeObject<OrgChartModel>(json);

Edit2: We're now running .NET 4.5. To the best of my knowledge, we ran 4.2 prior, but seeing it's been a few months, I cannot be sure.

As per comment by Dominick, I tried changing the cast to DefaultContractResolver to the following:

            var contractResolver =
          (IContractResolver)serializerSettings.ContractResolver;

This, however, then ends up in the API returning the following error:

{"Message":"The requested resource does not support http method 'GET'."}

like image 883
SchmitzIT Avatar asked Sep 01 '16 10:09

SchmitzIT


2 Answers

What I understand from this code is that you just try to set the IgnoreSerializableAttribute of your resolver to true.

1> Do you know why it has to be done? What are the effect if your remove it? I can see from the Newton Doc that setting IgnoreSerializableAttribute to true will stop Newtonsoft.Json from behaving like Microsoft's serialisers and instead just serialise the public properties.

Do you still need that?

2> What is the type of your current SerializerSettings (we only know it's not the default one, so you probably change it somewhere?) Are you sure it still has IgnoreSerializableAttribute to false by default? If so you probably has a way to reach this attribute using the actual type?

Hope it helps

like image 140
Ouarzy Avatar answered Sep 21 '22 15:09

Ouarzy


I managed to solve the issue by using a newer version of JSON.NET (8, where before we were using version 6). This resolved the error.

like image 23
SchmitzIT Avatar answered Sep 18 '22 15:09

SchmitzIT