-
Notifications
You must be signed in to change notification settings - Fork 191
Use object indirection in HttpContextAccessor #1066
Conversation
if (holder != null) | ||
{ | ||
// Kill current HttpContext trapped in the AsyncLocals, as its done. | ||
holder.Context = null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Clever!
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
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 |
It's an extra allocation per request but async locals aren't super light anyways so this is fine. |
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 toHttpContextHolder
at which point it will remain inert and they won't see data for the next request (as a newHttpContextHolder
will be created)