It seems that AspNet.Core starts sending response that is IEnumerable right away without iterating over the whole collection. E.g.:
[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.
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 e.g. 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 e.g. API project and standard JSON response.
I was not able to find anything in documentation that describes this behavior and how to prevent it.
P.S. I have verified that calling ToList() fixes the issue and the response is 500 with correct exception (with UseDeveloperExceptionPage)
ToList()each time with possibility of forgetting it and client getting an invalid json instead of 500. Any ideas on how to prevent it? I assume that this is because it starts responding stream, and that's why it is happening. If there are other reasons for it, it does not change the fact unfortunatelyToList()it?ToList()I get an expected 500. I have added it to the question