Skip to content

Why is AsyncLocal<HttpContextHolder> static? #26491

Answered by Tratcher
czb182 asked this question in General
Discussion options

You must be logged in to vote

It looks like HttpContextAccessor is registered as Singleton via helper method, so couldn't the AsyncLocal be non static, and would be basically the same?

Yes, but we've had issues with people registering it themselves not as a singleton. It turns out there's no reason to have it not be static.

Thinking further, if it were not static, couldnt it just be scoped? Would the context only need to be preserved across the request, not the lifetime of the app?

We've considered an alternate service that was scoped and didn't use async locals at all. It would be simpler and less error prone. Unfortunately people need access to the accessor service from other singleton services where you can't i…

Replies: 2 comments 4 replies

Comment options

You must be logged in to vote
0 replies
Answer selected by czb182
Comment options

You must be logged in to vote
4 replies
@Tratcher
Comment options

Tratcher Oct 2, 2020
Collaborator

@davidfowl
Comment options

@PMExtra
Comment options

@davidfowl
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
4 participants