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

Conversation

benaadams
Copy link
Contributor

@benaadams benaadams commented Nov 16, 2018

Use an extra level of indirection in HttpContextAccessor so what is being held onto is shared (rather than its own reference); so when the request is done that second indirection can be cleared for everything holding on to HttpContextHolder at which point it will remain inert and they won't see data for the next request (as a new HttpContextHolder will be created)

if (holder != null)
{
// Kill 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.

@davidfowl
Copy link
Member

Change the HttpContextFactory to not set the TraceIdentifier to null. Also tweak some of these tests https://github.com/aspnet/HttpAbstractions/pull/1021/files to avoid the TraceIdentifier comparison

@davidfowl
Copy link
Member

It's an extra allocation per request but async locals aren't super light anyways so this is fine.

@davidfowl davidfowl merged commit 49d785c into aspnet:master Nov 16, 2018
@benaadams benaadams deleted the HttpContextAccessor branch November 16, 2018 06:47
@Tratcher Tratcher added this to the 3.0.0 milestone Nov 16, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants