You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I use an IAsyncAlwaysRunResultFilter to execute some code as late as possible, but before the response is sent to the client. I assumed a result filter could be used for this.
I now have 2 different projects that use a nearly identical IAsyncAlwaysRunResultFilter implementation, but the behavior is different. In the first project, the filter reacts how I expected it to react and the filter code is consistently executed before a response is sent to the client. In the second project, the filter behaves differently depending on whether I execute the code before or after calling next.
I don't know whether this is relevant, but the first project does not have any custom middleware, the second project has a lot of custom middleware. Some of the middleware registers lambdas using Response.OnStarting. Some of the middleware shortcuts the middleware pipeline and directly writes a response.
So the code for the IAsyncAlwaysRunResultFilter is something like the following when the code is executed afterwards:
So, before or after has the same result on the first project: the response is only sent out after the result filter was executed completely.
In the second project, in the after case: as soon as the statement await next(); is executed, the response is already sent out and Response.HasStarted is true (naturally). In the before case however: the response is only sent out after the result filter was executed completely.
In the documentation on Filters in ASP.NET Core, I only find the following that might refer to this behavior:
When the Microsoft.AspNetCore.Mvc.Filters.IResultFilter.OnResultExecuted method runs,
the response has probably already been sent to the client. If the response has already been
sent to the client, it cannot be changed.
If I translate that to an async result filter, I understand that it means: when the await next(); statement is executed, the response has probably already been sent to the client.
I find the wording a bit confusing: what does probably mean? There must be a trigger for freezing the response and starting to send it out to the client? What is that trigger in the ASP.NET Core processing pipeline?
Can someone clarify what happens exactly? Is it guaranteed that the response has not been sent to the client in the before part of the result filter? Is it a bad idea to use a result filter for this, and should this be done using middleware and Response.Onstarting?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
Hello,
I use an
IAsyncAlwaysRunResultFilter
to execute some code as late as possible, but before the response is sent to the client. I assumed a result filter could be used for this.I now have 2 different projects that use a nearly identical
IAsyncAlwaysRunResultFilter
implementation, but the behavior is different. In the first project, the filter reacts how I expected it to react and the filter code is consistently executed before a response is sent to the client. In the second project, the filter behaves differently depending on whether I execute the code before or after callingnext
.I don't know whether this is relevant, but the first project does not have any custom middleware, the second project has a lot of custom middleware. Some of the middleware registers lambdas using
Response.OnStarting
. Some of the middleware shortcuts the middleware pipeline and directly writes a response.So the code for the
IAsyncAlwaysRunResultFilter
is something like the following when the code is executed afterwards:And when the code is executed before:
So, before or after has the same result on the first project: the response is only sent out after the result filter was executed completely.
In the second project, in the after case: as soon as the statement
await next();
is executed, the response is already sent out andResponse.HasStarted
is true (naturally). In the before case however: the response is only sent out after the result filter was executed completely.In the documentation on Filters in ASP.NET Core, I only find the following that might refer to this behavior:
If I translate that to an async result filter, I understand that it means: when the
await next();
statement is executed, the response has probably already been sent to the client.I find the wording a bit confusing: what does probably mean? There must be a trigger for freezing the response and starting to send it out to the client? What is that trigger in the ASP.NET Core processing pipeline?
Can someone clarify what happens exactly? Is it guaranteed that the response has not been sent to the client in the before part of the result filter? Is it a bad idea to use a result filter for this, and should this be done using middleware and
Response.Onstarting
?Thanks,
Ruben
Beta Was this translation helpful? Give feedback.
All reactions