diff --git a/eng/PatchConfig.props b/eng/PatchConfig.props
index 229a1c4e2f04..44b7a873f570 100644
--- a/eng/PatchConfig.props
+++ b/eng/PatchConfig.props
@@ -29,6 +29,7 @@ Later on, this will be checked using this condition:
@aspnet/signalr;
Microsoft.AspNetCore.Authentication.Google;
+ Microsoft.AspNetCore.Http;
diff --git a/src/Http/Http/src/HttpContextAccessor.cs b/src/Http/Http/src/HttpContextAccessor.cs
index 897c27f7345a..286151029cf6 100644
--- a/src/Http/Http/src/HttpContextAccessor.cs
+++ b/src/Http/Http/src/HttpContextAccessor.cs
@@ -7,21 +7,35 @@ namespace Microsoft.AspNetCore.Http
{
public class HttpContextAccessor : IHttpContextAccessor
{
- private static AsyncLocal<(string traceIdentifier, HttpContext context)> _httpContextCurrent = new AsyncLocal<(string traceIdentifier, HttpContext context)>();
+ private static AsyncLocal _httpContextCurrent = new AsyncLocal();
public HttpContext HttpContext
{
get
{
- var value = _httpContextCurrent.Value;
- // Only return the context if the stored request id matches the stored trace identifier
- // context.TraceIdentifier is cleared by HttpContextFactory.Dispose.
- return value.traceIdentifier == value.context?.TraceIdentifier ? value.context : null;
+ return _httpContextCurrent.Value?.Context;
}
set
{
- _httpContextCurrent.Value = (value?.TraceIdentifier, value);
+ var holder = _httpContextCurrent.Value;
+ if (holder != null)
+ {
+ // Clear current HttpContext trapped in the AsyncLocals, as its done.
+ holder.Context = null;
+ }
+
+ if (value != null)
+ {
+ // Use an object indirection to hold the HttpContext in the AsyncLocal,
+ // so it can be cleared in all ExecutionContexts when its cleared.
+ _httpContextCurrent.Value = new HttpContextHolder { Context = value };
+ }
}
}
+
+ private class HttpContextHolder
+ {
+ public HttpContext Context;
+ }
}
}
diff --git a/src/Http/Http/src/HttpContextFactory.cs b/src/Http/Http/src/HttpContextFactory.cs
index f293ef478231..8236a388a564 100644
--- a/src/Http/Http/src/HttpContextFactory.cs
+++ b/src/Http/Http/src/HttpContextFactory.cs
@@ -53,10 +53,6 @@ public void Dispose(HttpContext httpContext)
{
_httpContextAccessor.HttpContext = null;
}
-
- // Null out the TraceIdentifier here as a sign that this request is done,
- // the HttpContextAccessor implementation relies on this to detect that the request is over
- httpContext.TraceIdentifier = null;
}
}
}
\ No newline at end of file
diff --git a/src/Http/Http/test/HttpContextAccessorTests.cs b/src/Http/Http/test/HttpContextAccessorTests.cs
index c1521b1bc342..c224a66a7dc2 100644
--- a/src/Http/Http/test/HttpContextAccessorTests.cs
+++ b/src/Http/Http/test/HttpContextAccessorTests.cs
@@ -44,7 +44,6 @@ public async Task HttpContextAccessor_GettingHttpContextReturnsNullHttpContextIf
var accessor = new HttpContextAccessor();
var context = new DefaultHttpContext();
- context.TraceIdentifier = "1";
accessor.HttpContext = context;
var checkAsyncFlowTcs = new TaskCompletionSource