简体   繁体   English

如果在迭代IEnumerable(从控制器返回)时发生异常,则AspNet.Core返回200 OK且无效的Json

[英]AspNet.Core returns 200 OK and invalid Json if there is an exception while iterating an IEnumerable (returned from controller)

It seems that AspNet.Core starts sending response that is IEnumerable right away without iterating over the whole collection. 似乎AspNet.Core立即开始发送IEnumerable响应而不迭代整个集合。 Eg: 例如:

[HttpGet("")]
public async Task<IActionResult> GetData()
{
    IEnumerable<MyData> result = await _service.GetData();
    return Ok(result.Select(_mapper.MapMyDataToMyDataWeb));
}

Now there is an exception that happens during mapping of one of the elements, so I would assume a 500 response, but in reality what happens is that I get a 200 with only partial (and incorrect) Json. 现在在映射其中一个元素时会发生异常,因此我会假设500响应,但实际上发生的是我得到的200只有部分(和不正确)的Json。

I assume it's a feature and not a bug in Asp.Net Core that provides this behavior and it is additionally relatively easy to fix by calling eg ToList() , but I am wondering if there is some kind of flag that can prevent this situation from happening since it does not really make sense for eg API project and standard JSON response. 我认为它是一个功能而不是Asp.Net Core一个提供此行为的错误,并且通过调用例如ToList()还可以相对容易地修复,但我想知道是否存在某种可以防止这种情况的标志发生,因为它对于例如API项目和标准JSON响应没有意义。

I was not able to find anything in documentation that describes this behavior and how to prevent it. 我无法在描述此行为的文档中找到任何内容以及如何防止它。

PS I have verified that calling ToList() fixes the issue and the response is 500 with correct exception (with UseDeveloperExceptionPage ) PS我已经验证调用ToList()修复了问题,响应为500且正确异常(使用UseDeveloperExceptionPage

It seems that this is actually "by design", this issue was raised few times on Asp.Net Core github repository. 看来这实际上是“按设计”,这个问题在Asp.Net Core github存储库中引发了几次。

What happens is that header with 200 is already sent, while the body is not. 会发生的是,已经发送了带有200的标头,而不是正文。 While I would think that enumeration must proceed before sending headers, asp.net team says it will use more resources on the server and that's why it is like that. 虽然我认为枚举必须在发送标题之前进行,但asp.net团队表示它将在服务器上使用更多资源,这就是为什么它就是这样。

Here is a quote: 这是一个引用:

It is very likely the case that your exception is thrown while writing to the body, after headers have already been sent to the client, so there's no take-backs on the 200 that was already sent as part of the response. 在标题已经发送到客户端之后,很可能会在写入正文时抛出异常,因此在响应的一部分已经发送的200上没有回收。 The client will see an error because the body will come back as incomplete. 客户端将看到错误,因为正文将返回不完整。

If you want to deterministically report a 500 when this happens you'll need to either: 如果你想确定性地报告500,当你发生这种情况时,你需要:

I can confirm that this solution worked: 我可以确认这个解决方案有效:

  1. Reference Microsoft.AspNetCore.Buffering package 参考Microsoft.AspNetCore.Buffering
  2. Write app.UseResponseBuffering() before app.UseMvc() app.UseResponseBuffering()之前写app.UseMvc()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么 Automapper 在映射列表时返回 null<t> 在 aspnet.core 3.1 中?</t> - Why Automapper returns null while mapping List<T> in aspnet.core 3.1? ASPNet.Core 从引用的程序集中读取 appsettings? - ASPNet.Core reading appsettings from referenced assemblies? 抛出异常的 IEnumerable 在 ASP.NET 核心 Web ZDB93474238D1043CACE1 中的 OkResult 传递时返回无效的 Json - IEnumerable that throws exception returns invalid Json when passed into OkResult in ASP.NET Core Web API 用于集成测试的ASPNet.Core HostingEnvironment? - ASPNet.Core HostingEnvironment for Integration Tests? 如何将.NetFramework 中的MessageHandler 转换为AspNet.Core - How to convert MessageHandler in .NetFramework to AspNet.Core 为什么 Aspnet.Core model 验证不会在 Nullable 属性上引发异常? - Why does not Aspnet.Core model validation throw an exception on Nullable properties? .net core HTTPS 请求返回 502 bad gateway 而 Postman 返回 200 OK - .net core HTTPS requests returns 502 bad gateway while Postman returns 200 OK postAsync 返回状态为 200,好的,但是在 postman 中返回数据时 response.content 为空 - postAsync returns status as 200, ok but response.content is empty while data is returned in postman .Net Core MVC - 无法获得406 - 不可接受,总是用Json返回200 OK - .Net Core MVC - cannot get 406 - Not Acceptable, always returns 200 OK with Json 如何在 AspNet.Core 中读取 EXIF 数据 - How can i read EXIF data in AspNet.Core
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM