Skip to content
This repository was archived by the owner on Nov 20, 2018. It is now read-only.

Use object indirection in HttpContextAccessor #1066

Merged
merged 4 commits into from
Nov 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions src/Microsoft.AspNetCore.Http/HttpContextAccessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<HttpContextHolder> _httpContextCurrent = new AsyncLocal<HttpContextHolder>();

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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clever!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the context starts throwing dotnet/aspnetcore#3586; then it wouldn't necessarily have to clear it here?

The change of holder (below) would still prevent it being shared across requests.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

People use null to detect if they are in a request aware place.

}

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;
}
}
}
4 changes: 0 additions & 4 deletions src/Microsoft.AspNetCore.Http/HttpContextFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
11 changes: 1 addition & 10 deletions test/Microsoft.AspNetCore.Http.Tests/HttpContextAccessorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<object>(TaskCreationOptions.RunContinuationsAsynchronously);
Expand Down Expand Up @@ -76,7 +75,6 @@ public async Task HttpContextAccessor_GettingHttpContextReturnsNullHttpContextIf

// Null out the accessor
accessor.HttpContext = null;
context.TraceIdentifier = null;

waitForNullTcs.SetResult(null);

Expand All @@ -86,12 +84,11 @@ public async Task HttpContextAccessor_GettingHttpContextReturnsNullHttpContextIf
}

[Fact]
public async Task HttpContextAccessor_GettingHttpContextReturnsNullHttpContextIfDifferentTraceIdentifier()
public async Task HttpContextAccessor_GettingHttpContextReturnsNullHttpContextIfChanged()
{
var accessor = new HttpContextAccessor();

var context = new DefaultHttpContext();
context.TraceIdentifier = "1";
accessor.HttpContext = context;

var checkAsyncFlowTcs = new TaskCompletionSource<object>(TaskCreationOptions.RunContinuationsAsynchronously);
Expand Down Expand Up @@ -121,12 +118,8 @@ public async Task HttpContextAccessor_GettingHttpContextReturnsNullHttpContextIf

await checkAsyncFlowTcs.Task;

// Reset the trace identifier on the first request
context.TraceIdentifier = null;

// Set a new http context
var context2 = new DefaultHttpContext();
context2.TraceIdentifier = "2";
accessor.HttpContext = context2;

waitForNullTcs.SetResult(null);
Expand All @@ -142,7 +135,6 @@ public async Task HttpContextAccessor_GettingHttpContextDoesNotFlowIfAccessorSet
var accessor = new HttpContextAccessor();

var context = new DefaultHttpContext();
context.TraceIdentifier = "1";
accessor.HttpContext = context;

var checkAsyncFlowTcs = new TaskCompletionSource<object>(TaskCreationOptions.RunContinuationsAsynchronously);
Expand Down Expand Up @@ -172,7 +164,6 @@ public async Task HttpContextAccessor_GettingHttpContextDoesNotFlowIfExecutionCo
var accessor = new HttpContextAccessor();

var context = new DefaultHttpContext();
context.TraceIdentifier = "1";
accessor.HttpContext = context;

var checkAsyncFlowTcs = new TaskCompletionSource<object>(TaskCreationOptions.RunContinuationsAsynchronously);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,13 @@ public void DisposeHttpContextSetsHttpContextAccessorToNull()

// Act
var context = contextFactory.Create(new FeatureCollection());
var traceIdentifier = context.TraceIdentifier;

// Assert
Assert.Same(context, accessor.HttpContext);

contextFactory.Dispose(context);

Assert.Null(accessor.HttpContext);
Assert.NotEqual(traceIdentifier, context.TraceIdentifier);
}

[Fact]
Expand Down