-
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? I'm trying to understand why we are setting a certain lifetime via static keyword, when we can set via DI as singleton. 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? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
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.
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 inject scoped services. |
Beta Was this translation helpful? Give feedback.
-
Thanks, that makes sense. I hadn't thought about it not needing to be AsyncLocal if could always reference Scoped instance, and I see issue about resolving in Singleton instance. While its on my mind, do you know more about why the object indirection is needed, as in comments? Why put the HttpContext inside another class, instead of having AsyncLocal? Is it just something special about HttpContext needed to be nulled out before overwriting, or is there a more generic issue? |
Beta Was this translation helpful? Give feedback.
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.
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…