title: "Unable to cast object of type 'System.Net.Http.Formatting.JsonContractResolver' to type 'Newtonsoft.Json.Serialization.DefaultContractResolver'."
body: <p>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:</p>
  
  <blockquote>
    <p>Unable to cast object of type
    'System.Net.Http.Formatting.JsonContractResolver' to type
    'Newtonsoft.Json.Serialization.DefaultContractResolver'.</p>
  </blockquote>
  
  <p>The offending line of code is in global.asax:</p>
  
  <pre><code>  protected void Application_Start() {
          GlobalConfiguration.Configure(WebApiConfig.Register);
  
          var serializerSettings =
           GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings;
          var contractResolver =
             (DefaultContractResolver)serializerSettings.ContractResolver;
             contractResolver.IgnoreSerializableAttribute = true;
       }
  </code></pre>
  
  <p>I believe this code was added because the default output of the API was XML, and we need it to be JSON instead. </p>
  
  <p>Highlighting <code>(DefaultContractResolver)</code> brings up a tooltip indicating it references <code>NewtonSoft.JSon.Serialization.DefaultContractResolver</code>. Highlighting serializersettings.ContractResolver references <code>IContractResolver JSonSerializerSettings.ContractResolver</code>. </p>
  
  <p>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. </p>
  
  <p>What could cause this line of code to suddenly throw an error? And how can I resolve it?</p>
  
  <p>Thanks!</p>
  
  <p>Edit: As per request in the comments, my serialization code consists of something like the following:</p>
  
  <pre><code>json += "{\"employeename\": \"" + Convert.ToString(reader["Employee"])
  + "\"},";
  
  return JsonConvert.DeserializeObject&lt;OrgChartModel&gt;(json);
  </code></pre>
  
  <p>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. </p>
  
  <p>As per comment by Dominick, I tried changing the cast to DefaultContractResolver to the following:</p>
  
  <pre><code>            var contractResolver =
            (IContractResolver)serializerSettings.ContractResolver;
  </code></pre>
  
  <p>This, however, then ends up in the API returning the following error:</p>
  
  <p><code>{"Message":"The requested resource does not support http method 'GET'."}</code></p>