2

We are using HttpClient extensions to call a web service from a web app on visual studio 2013. Sometimes when we call PostAsJsonAsync method, we get this error "Method not found: 'Void Newtonsoft.Json.Serialization.DefaultContractResolver.set_IgnoreSerializableAttribute(Boolean)'" Our code is as follows:

using (var client = new HttpClient())
{
    client.SetAuthenticationHeader();
    client.SetServiceBaseAddress();

    var response = await client.PostAsJsonAsync("publishfile/", publishFileDto); 

    if (response.IsSuccessStatusCode)
    {
        RedirectToAction("Certificates");
    }

    ModelState.AddModelError("File", "Failed to upload file");
}

We have tried a lot of solutions offered on stackoverflow and none of them worked. We have upgraded all dependency libraries to latest version, especially Microsoft.AspNet.WebApi.Client and Newtonsoft.Json, we also tried downgrading, and still we had no luck. I also opened the packages folder in explorer and made sure that no library has multiple versions that are not used, for instance, Newtosoft.Json had many version from 6.0.1 to 7.0.1.

We tried these proposed solutions:

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

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

HttpClient not supporting PostAsJsonAsync method C#

I also checked this:

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

which did not help much.

Below is part of the stack trace info.

[MissingMethodException: Method not found: 'Void Newtonsoft.Json.Serialization.DefaultContractResolver.set_IgnoreSerializableAttribute(Boolean)'.]
System.Net.Http.Formatting.JsonContractResolver..ctor(MediaTypeFormatter formatter) +0 
System.Net.Http.Formatting.BaseJsonMediaTypeFormatter..ctor() +84 
System.Net.Http.Formatting.JsonMediaTypeFormatter..ctor() +113
System.Net.Http.HttpClientExtensions.PostAsJsonAsync(HttpClient client, String requestUri, T value, CancellationToken cancellationToken) +61
System.Net.Http.HttpClientExtensions.PostAsJsonAsync(HttpClient client, String requestUri, T value) +132 

The weird thing is that sometimes, the exception does not get thrown, the object gets serialized and sent to the web service, and everything works fine. Later when we execute the same code again we get the exception.

We have reached a point where we want to consider a different library to make web api calls.

Community
  • 1
  • 1
Alec BW
  • 243
  • 2
  • 18

0 Answers0