question

AJ-8693 avatar image
0 Votes"
AJ-8693 asked DuaneArnold-0443 commented

Web API c#.net Response 500 null reference json serialization

Hi Team,

We are using web APIs in our project, The time we access first web API from our project we get a 500 error as a response. once we restart the app pool on the machine, the error goes away and everything works fine.

It occurs very rarely. So, it's very hard to reproduce. error is invoked on its own

As per our current investigation, we noted once in our local machine while debugging found the attached exception.

We get a null reference exception while serializing objects to JSON. check attachment for info

The issue occurs very rarely on both server(Windows Server 2019) and local window machine
.net framework version being used with this mvc c#.net webapi is 4.5.1

Following things we have tried : -

  1. Upgraded the json version to Newtonsoft.json.13.0.1

  2. Update the response to a mvc model from as earlier we were sending entity

  3. Globally we have setup the below line in webapiconfig file
    config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;




dotnet-csharp
error500.png (779.2 KiB)
· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


Probably it is not totally improbable that there is an issue in the serialization library, but you can also try the new System.Text.Json NuGet package (available for .NET Framework and .NET), unless you need some specific things.

0 Votes 0 ·

I am creating different solutions for this issue.. One of them is this one i.e. using system.text.json . After deploying will so how things perform

0 Votes 0 ·

I suspect there is something wrong with the object you're trying to Json serialize/deserialize which is not uncommon. I have seen it in XML serialization/deserialization of an object where the client was using a different .NET Framework than what the provider was using that serialized the object.

0 Votes 0 ·

Also reconsider the thread-safety of stuffs related to converters: https://github.com/JamesNK/Newtonsoft.Json/pull/1679.


0 Votes 0 ·

1 Answer

DuaneArnold-0443 avatar image
0 Votes"
DuaneArnold-0443 answered
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.