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

Make HttpContextAccessor static AsyncLocal #753

Merged
merged 1 commit into from
Jan 10, 2017
Merged

Make HttpContextAccessor static AsyncLocal #753

merged 1 commit into from
Jan 10, 2017

Conversation

Tratcher
Copy link
Member

@Tratcher Tratcher commented Jan 9, 2017

#723 The .NET 4.5.1 version is static but the .NETStandard1.3 version isn't, which can cause problems if you end up with different instances of HttpContextAccessor.

@Tratcher Tratcher added this to the 2.0.0 milestone Jan 9, 2017
@Tratcher Tratcher self-assigned this Jan 9, 2017
@Tratcher
Copy link
Member Author

Tratcher commented Jan 9, 2017

@dotnetjunkie

@Tratcher Tratcher merged commit 622d112 into dev Jan 10, 2017
@Tratcher Tratcher deleted the tratcher/static branch January 10, 2017 00:05
@davidfowl
Copy link
Member

Part of me doesn't like this change. The http context accessor should be a singleton, newing it up now shares the async local state with all other instances.

@dotnetjunkie Remind me how you ran into this.

@dotnetjunkie
Copy link

@davidfowl, try not to act purely on feelings. That typically not works out well :)

Here's a simple way to reproduce the issue:

services.AddTransient<IHttpContextAccessor, HttpContextAccessor>();

@davidfowl
Copy link
Member

The inconsistency is an issue but I'd rather make .NET Framework act like .NET Core than vice versa. Adding a transient IHttpContextAccessor is just broken.

@dotnetjunkie
Copy link

Yes, making that component transient is clearly broken, but by no way does the framework guide developers into the right direction.

It is really easy for a user to do the wrong thing, while it is really hard for the user to track down the bug that is the result of this. I already shown two completely different scenarios (both in the previous comment and in #723) that can cause bugs.

@davidfowl
Copy link
Member

Agreed but that can be solved in other ways. I'm just of the opinion this is the wrong fix for this particular problem. There are been some huge improvements made to the allocation profile to async locals in .NET Core vnext so maybe it's time to turn this back on by default again and measure. Also dropping .NET 4.5.1 (and rebasing to .NET Framework 4.6) support would let us use async local on both .NET Core and .NET Framework.

That solves both the lifetime issue and saves the implementation from hard coding statics.

@dotnetjunkie
Copy link

The only actual fix here is to make the AsyncLocal field static, because it's very easy for a user to accidentally create a second instance of the HttpContextAccessor as explained in #723.

@davidfowl
Copy link
Member

The only actual fix here is to make the AsyncLocal field static, because it's very easy for a user to accidentally create a second instance of the HttpContextAccessor as explained in #723.

I'm not convinced that would be common if we were able to add this service back to hosting by default.

@davidfowl
Copy link
Member

I mentioned it here #753 (comment)

@Tratcher
Copy link
Member Author

As a data point, we have many uses of AsyncLocal and most of them are static.
https://github.com/search?q=org%3Aaspnet+AsyncLocal&type=Code

@davidfowl how much of your concern applies to these other cases?

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.

5 participants