-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Cannot write to Response.Body in 1.1 #5555
Comments
Thanks for reporting this. It appears we're missing a check there. We'll look in to getting this fixed in the next patch release. |
@nrandell Could you share more details about your app? Example the action code that you had earlier etc. The mentioned code in SaveTempDataFilter happens when executing an action result and if you were writing to the response stream directly, you might not have been returning an action result, so wanted to check. Also I did a quick verification and couldn't repro it. |
This was my original code that worked before the upgrade. It basically downloads a blob from azure storage and streams that back to the client.
In trying to get it to work I made many changes, but ultimately ended up with
|
Thanks @nrandell I was able to repro it. As @DamianEdwards mentioned, there should be a check here for response already started or not |
Whats the recommended workaround for this? The error is causing my response to end prematurely. |
@scrouchman Following is a workaround that you can use: Assuming that you are hitting this exception when you are trying to write to the response stream directly in an action for example, you can do the following: class StreamActionResult : IActionResult
{
private readonly Func<HttpResponse, Task> _writeToResponseFunc;
public StreamActionResult(Func<HttpResponse, Task> writeToResponseFunc)
{
if(writeToResponseFunc == null)
{
throw new ArgumentNullException(nameof(writeToResponseFunc));
}
_writeToResponseFunc = writeToResponseFunc;
}
public Task ExecuteResultAsync(ActionContext context)
{
return _writeToResponseFunc(context.HttpContext.Response);
}
} Now you can use this in a controller's action like below: public IActionResult WriteToStreamFromAction()
{
return new StreamActionResult((response) =>
{
return response.WriteAsync("Hello World");
});
} The reason this works is because the func provided in the action is not executed until the StreamActionResult is executed. If your scenario is different than my assumption, could you share it? |
Thanks for this. Your assumption is mostly right, though I'm writing bytes directly to Response.Body. This new action result does indeed fix the issue and is definitely nicer than removing the filter |
@scrouchman you can also safely just return a stream from your action method. Or use https://github.com/aspnet/Mvc/search?utf8=%E2%9C%93&q=StreamOutputFormatter |
This patch bug is approved. Please use the normal code review process w/ a PR and make sure the fix is in the correct branch, then close the bug and mark it as done. |
I discussed this with @rynowak and we think that the correct solution here is to move the OnStarting code into |
Yup, sounds like a good plan. |
I've just updated an app to version 1.1 and some code that was working started throwing exception saying
It turns out that the issue was down to writing to Response.Body in my MVC actions. I've worked around this by downloading to a byte array and then returning File(array, contentType).
However I could not easily find any reason behind this - is this an error, desired functionality or just something wrong I'm doing?
The text was updated successfully, but these errors were encountered: