ASP.NET web api 2 截断 json 响应
ASP.NET web api 2 truncating json response
我的 API 通话遇到了一个非常奇怪的间歇性问题。有时 响应会被截断 。
- 没有关于如何或何时截断的模式。
- 响应大小为 200kb,配置的响应限制为 20mb。
格式化程序配置:
private static HttpConfiguration ConfigureFormatters(this HttpConfiguration config)
{
config.Formatters.Clear();
config.Formatters.Add(new JsonMediaTypeFormatter());
config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
return config;
}
简化的Api调用:
[HttpGet, Route]
public IHttpActionResult Explore(int cityId)
{
var lists = exploreBuilderService.Build(cityId);
return Ok(lists);
}
lists
类型是 List<SomeModel>
。 SomeModel
是 DTO
,没有循环引用。
响应示例:
Response Headers
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/10.0
X-Powered-By: ASP.NET
Date: Fri, 17 Jan 2020 12:02:37 GMT
Content-Length: 290248
Data
[..., {"id":47
...
是数组中的其他对象。
看起来 json 响应字符串被随机截断了。当这样的响应出现时,我们的 android 应用程序失败并返回 MalformedJsonException
。它发生在对同一数据集的 5-10 个请求上。
你遇到过这样的问题吗?我应该在哪里寻找导致这种情况的潜在问题?
问题出在我们的中间件之一,它进行了响应拦截。
也许现在有人会遇到这个问题,就像我一样。
但事实证明,.Net 6
An ASP.NET Core web API endpoint could return a truncated JSON body with a 200 response code when an exception is thrown during serialization of the object returned by the action method if a sufficient portion of the response has already been successfully serialized beforehand, so that part of the response has already been sent to the client. Fortunately, the exception will still be logged properly. The only way to reliably prevent this is to ensure that the returned object can be successfully serialized.
我的 API 通话遇到了一个非常奇怪的间歇性问题。有时 响应会被截断 。
- 没有关于如何或何时截断的模式。
- 响应大小为 200kb,配置的响应限制为 20mb。
格式化程序配置:
private static HttpConfiguration ConfigureFormatters(this HttpConfiguration config)
{
config.Formatters.Clear();
config.Formatters.Add(new JsonMediaTypeFormatter());
config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
return config;
}
简化的Api调用:
[HttpGet, Route]
public IHttpActionResult Explore(int cityId)
{
var lists = exploreBuilderService.Build(cityId);
return Ok(lists);
}
lists
类型是 List<SomeModel>
。 SomeModel
是 DTO
,没有循环引用。
响应示例:
Response Headers
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/10.0
X-Powered-By: ASP.NET
Date: Fri, 17 Jan 2020 12:02:37 GMT
Content-Length: 290248
Data
[..., {"id":47
...
是数组中的其他对象。
看起来 json 响应字符串被随机截断了。当这样的响应出现时,我们的 android 应用程序失败并返回 MalformedJsonException
。它发生在对同一数据集的 5-10 个请求上。
你遇到过这样的问题吗?我应该在哪里寻找导致这种情况的潜在问题?
问题出在我们的中间件之一,它进行了响应拦截。
也许现在有人会遇到这个问题,就像我一样。 但事实证明,.Net 6
An ASP.NET Core web API endpoint could return a truncated JSON body with a 200 response code when an exception is thrown during serialization of the object returned by the action method if a sufficient portion of the response has already been successfully serialized beforehand, so that part of the response has already been sent to the client. Fortunately, the exception will still be logged properly. The only way to reliably prevent this is to ensure that the returned object can be successfully serialized.