12

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'."}

SchmitzIT
  • 9,227
  • 9
  • 65
  • 92
  • can you please add your json deserialization code ? I have doubt that you have used deserialization directly to dynamic object rather then using classes. – Pranav Patel Jan 11 '17 at 10:05
  • I think the error comes up in the SerializerSettings, because the return class is `IContractResolver JSonSerializerSettings.ContractResolver` and than you want to `cast`it as `(DefaultContractResolver)` which is allready included in the `NewtonSoft.JSon dll` that you include. So you need to specify your `DefaultContractResolver` as your own, not the dll ones. – DomeTune Jan 11 '17 at 10:10
  • @PranavPatel - Done. Does that help? :) – SchmitzIT Jan 11 '17 at 10:12
  • 1
    what version of json.net, .net and what version of web api are you running? – The Bearded Llama Jan 11 '17 at 10:13
  • and what version of .NET you _was_ running before update? – Dmitry Jan 11 '17 at 10:14
  • @TheBeardedLlama Done for .NET. I'm not sure how I can find the version of WebAPI, though. – SchmitzIT Jan 11 '17 at 10:27
  • I would recommend that you find out what versions you're using first: .net, asp.net, json.net and web api – The Bearded Llama Jan 11 '17 at 10:35
  • 1
    the versions are important, because otherwise no one can help you and you will get crap answers because of the high bounty – The Bearded Llama Jan 11 '17 at 10:37
  • also have a look at http://stackoverflow.com/questions/28552567/web-api-2-how-to-return-json-with-camelcased-property-names-on-objects-and-the there are **loads** of questions about how to change the contract resolver in web api on stackoverflow – The Bearded Llama Jan 11 '17 at 10:38
  • also http://stackoverflow.com/questions/24543385/where-should-i-plug-in-my-custom-defaultcontractresolver-json-net – The Bearded Llama Jan 11 '17 at 10:43
  • 2
    @TheBeardedLlama Thanks. I didn't realize this was a high bounty, lol. I seem to have circumvented the issue now by using a higher version of JSON.NET (8.* rather than 6.*), deleting the DLL on the server, and republishing. That seems to work (for now, fingers crossed). – SchmitzIT Jan 11 '17 at 13:09
  • ah yes that v of json.net is too old, so it makes sense – The Bearded Llama Jan 11 '17 at 13:35
  • I was going to say, since it broke when you changed machines it was executing on, it sounded like a "Well it works on my machine" problem, meaning that a version of _something_ changed between where it was running, and where it is running now. – krillgar Jan 16 '17 at 17:48
  • You should probably make your own answer, since you solved it yourself. Just accept it when you can. Anything anyone else will submit will either be a duplicate of your answer, or completely irrelevant. – krillgar Jan 16 '17 at 17:49
  • @krillgar Thanks. I intended to do that yesterday but got sidetracked with work. Done now, though. – SchmitzIT Jan 17 '17 at 08:31

2 Answers2

5

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

Ouarzy
  • 3,015
  • 1
  • 16
  • 19
  • Hi. 1) If I do not use the setting, the output simply won't show in the format I need it. I cannot remember exactly what, and I'm reluctant to change things because it's working again now. – SchmitzIT Jan 16 '17 at 10:23
  • 2) I don't know. I (to the best of my knowledge) do not have any other code referencing the SerializerSettings anywhere, other than in global.asax. – SchmitzIT Jan 16 '17 at 10:24
2

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.

SchmitzIT
  • 9,227
  • 9
  • 65
  • 92