core icon indicating copy to clipboard operation
core copied to clipboard

Http response JSON gets truncated

Open ayazaliuk101 opened this issue 8 years ago • 17 comments

Hi we are getting a strange behavior with ASP.net core 2. We have created a simple web API that returns a large amount of JSON data (3.7MB) read from a static JSON file asynchronously. We have found that when we apply load on this application hosted in Azure web apps (> 50 requests) some of the responses contains JSON that is truncated. i.e. we get a status of 200 however the JSON is malformed and truncated. Our original hypothesis we thought was due to the size of the data and and have implemented compression to dis/prove this, however this issue still persists. We have found that this issue does not occur when the size of the Json is less than 1.5 MB and can handle requests > 250.We have also found that when the server resources are increased, i.e. we scale up or out on Azure (from basic) it reduces the frequency it occurs but does not remove it altogether.

To add to the above, we have carried out a similar exercise using ASP.net MVC and we do not get this behavior. The MVC application was also hosted on the same web app.

ayazaliuk101 avatar Jan 25 '18 10:01 ayazaliuk101

@Eilon

Petermarcu avatar Mar 18 '18 17:03 Petermarcu

Can you reproduce this locally?

davidfowl avatar Mar 18 '18 18:03 davidfowl

@Davidfowl yes. Also sent the details to MS support with the source code and they can reproduce on their side as well. Need to run more logs for them to escalate to .net core team.

ayazaliuk101 avatar Mar 18 '18 21:03 ayazaliuk101

Apologies when you say locally i.e. on my local machine then no, but I can reproduce on a Azure web app.

ayazaliuk101 avatar Mar 18 '18 21:03 ayazaliuk101

@Eilon @davidfowl should this issue get moved to an ASP.NET repo?

Petermarcu avatar Mar 23 '18 15:03 Petermarcu

@Petermarcu I think that isn't yet clear at this time. Could be an Azure issue.

@ayazaliuk101 - have you been able to find any more info on this?

Eilon avatar Mar 23 '18 18:03 Eilon

the similar issue , it is generating while debugging ..

screenshot_7

tharmachandran avatar Apr 11 '18 05:04 tharmachandran

Resolved by this ref .

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc()
        .AddJsonOptions(
            options => options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
        );
}

tharmachandran avatar Apr 11 '18 05:04 tharmachandran

This issue was moved to aspnet/Home#3047

Eilon avatar Apr 11 '18 20:04 Eilon

I had the same problem, so thanks @tharmachandran for the Json options statement. Fixed the problem for me

TrevorFradsham avatar Dec 03 '18 19:12 TrevorFradsham

I also had the same problem and @tharmachandran 's fix worked for me too, thanks for chiming in @TrevorFradsham .

NeilMcKLogic avatar Jan 21 '19 23:01 NeilMcKLogic

You can try to increase the size of request JSON. Have a look at link below https://www.talkingdotnet.com/how-to-increase-file-upload-size-asp-net-core/

kizora avatar Feb 07 '19 12:02 kizora

For anyone else landing here from a search on this issue:

One reason this can happen is if an error happens while serializing the object to JSON. For example a field marked as required that is not present in the data can cause this. The output simply stops and no exception is reported in this case.

Check that you can serialize the json object using JSonConvert.SerializeObject() before returning it and fix up any issues you find.

IanMercer avatar Aug 08 '19 14:08 IanMercer

@IanMercer thank you very very much you helpd solving my problem I was working on for two days.

schufeli avatar Oct 31 '19 10:10 schufeli

In my case this solve my issue on core 3, by this: https://docs.microsoft.com/en-us/aspnet/core/web-api/advanced/formatting?view=aspnetcore-3.0#add-newtonsoftjson-based-json-format-support

Prior to ASP.NET Core 3.0, the default used JSON formatters implemented using the Newtonsoft.Json package. In ASP.NET Core 3.0 or later, the default JSON formatters are based on System.Text.Json. Support for Newtonsoft.Json based formatters and features is available by installing the Microsoft.AspNetCore.Mvc.NewtonsoftJson NuGet package and configuring it in Startup.ConfigureServices.

public void ConfigureServices(IServiceCollection services) { services.AddControllers() .AddNewtonsoftJson(); }

proteo5 avatar Nov 30 '19 20:11 proteo5

My scenario was .NET Core 3.1, Newtonsoft Json, and a custom middleware to change the response body. The solution described at https://stackoverflow.com/a/62882787/3142593 worked for me.

hsupe avatar Mar 25 '21 20:03 hsupe

For anyone else landing here from a search on this issue:

One reason this can happen is if an error happens while serializing the object to JSON. For example a field marked as required that is not present in the data can cause this. The output simply stops and no exception is reported in this case.

Check that you can serialize the json object using JSonConvert.SerializeObject() before returning it and fix up any issues you find.

This solved my problem. Thank you @IanMercer!

gelanquizora avatar Jan 26 '23 01:01 gelanquizora