Inline second statemahine into loop and us EC.Restore to reset context (#23175)
This commit is contained in:
parent
de726ccc11
commit
03b75e354d
|
|
@ -607,6 +607,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http
|
||||||
|
|
||||||
private async Task ProcessRequests<TContext>(IHttpApplication<TContext> application)
|
private async Task ProcessRequests<TContext>(IHttpApplication<TContext> application)
|
||||||
{
|
{
|
||||||
|
var cleanContext = ExecutionContext.Capture();
|
||||||
while (_keepAlive)
|
while (_keepAlive)
|
||||||
{
|
{
|
||||||
BeginRequestProcessing();
|
BeginRequestProcessing();
|
||||||
|
|
@ -637,26 +638,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http
|
||||||
|
|
||||||
InitializeBodyControl(messageBody);
|
InitializeBodyControl(messageBody);
|
||||||
|
|
||||||
// We run user controlled request processing in a seperate async method
|
|
||||||
// so any changes made to ExecutionContext are undone when it returns and
|
|
||||||
// each request starts with a fresh ExecutionContext state.
|
|
||||||
await ProcessRequest(application);
|
|
||||||
|
|
||||||
// Even for non-keep-alive requests, try to consume the entire body to avoid RSTs.
|
|
||||||
if (!_connectionAborted && _requestRejectedException == null && !messageBody.IsEmpty)
|
|
||||||
{
|
|
||||||
await messageBody.ConsumeAsync();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (HasStartedConsumingRequestBody)
|
|
||||||
{
|
|
||||||
await messageBody.StopAsync();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private async ValueTask ProcessRequest<TContext>(IHttpApplication<TContext> application)
|
|
||||||
{
|
|
||||||
var context = application.CreateContext(this);
|
var context = application.CreateContext(this);
|
||||||
|
|
||||||
try
|
try
|
||||||
|
|
@ -742,6 +724,21 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http
|
||||||
}
|
}
|
||||||
|
|
||||||
application.DisposeContext(context, _applicationException);
|
application.DisposeContext(context, _applicationException);
|
||||||
|
|
||||||
|
// Even for non-keep-alive requests, try to consume the entire body to avoid RSTs.
|
||||||
|
if (!_connectionAborted && _requestRejectedException == null && !messageBody.IsEmpty)
|
||||||
|
{
|
||||||
|
await messageBody.ConsumeAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (HasStartedConsumingRequestBody)
|
||||||
|
{
|
||||||
|
await messageBody.StopAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clear any AsyncLocals set during the request; back to a clean state ready for next request
|
||||||
|
ExecutionContext.Restore(cleanContext);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void OnStarting(Func<object, Task> callback, object state)
|
public void OnStarting(Func<object, Task> callback, object state)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue