3

Using the PutAsJsonAsync extension method for HttpClient in an asp.net mvc 5 returns a Self referencing loop detected exception.

Here is the calling code:

httpClient.BaseAddress = _uri;
HttpResponseMessage response = await httpClient.PutAsJsonAsync<b>("index/1",b);
response.EnsureSuccessStatusCode();

The object b does have a self reference.

So my question is how do I set SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore in an asp.net mvc 5 application.

Dug
  • 825
  • 1
  • 9
  • 19
  • You might have a look at my answer on **[“Self Referencing Loop Detected” exception with JSON.Net](https://stackoverflow.com/questions/40472419/self-referencing-loop-detected-exception-with-json-net/51235783#51235783)** page. – Murat Yıldız Jul 08 '18 at 20:37

1 Answers1

4

One way to solve this problem is to change from using the PutAsJsonAsync extension method to using the PutAsync extension method and setting the MediaTypeformatter explicitly.

var jsonformatter = new JsonMediaTypeFormatter();
jsonformatter.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;

HttpResponseMessage response = await httpClient.PutAsync<b>("index/1",b,jsonformatter);
response.EnsureSuccessStatusCode();

This allows you to use whatever setting you need.

Dug
  • 825
  • 1
  • 9
  • 19