This repository has been archived by the owner on Nov 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 193
HttpContextAccessor behaves differently under .NETStandard1.3 vs .NET 4.5.1 #723
Comments
Thats a good find. What were you doing that raised this problem? |
I was looking at the configuration for a .NET Core application of a Simple Injector user. He actually registered that Accessor as singleton where Simple Injector created that instance. This made me trigger looking at the source code of the Accessor where I spotted this bug. |
ActionContextAccessor seems to have the same issue. I presumed this was intentional and spent most of yesterday working round it :\ New bug report required? |
We'll fix this in the next release but the issue is pretty easy to avoid. |
Yes please file a matching bug in MVC. |
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
The
HttpContextAccessor
class (see here) implementation contains a compiler directive that contains a different implementation for .NETStandard1.3. The .NET 4.5.1 implementation usesCallContext
, while the .NETStandard1.3 version usesAsyncLocal<T>
under the covers.These two versions however behave differently. While the .NET 4.5.1 version of
HttpContextAccessor
is stateless, the .NETStandard version contains state in the form o an instance field. This could potentially cause a system, when deployed under .NET 4.5.1, to run fine, while completely breaking when running under .NETStandard1.3.This will happen when an application, or third party library creates its own instance of the
HttpContextAccessor
. While the stateless .NET 4.5.1 version will always request theHttpContext
from theCallContext
, the .NETStandard version will create a new async-local container per class instance. This will cause ASP.NET Core to set the HttpContext in a different bucket then where the the application or 3rd party library will read from (since it contains its ownAsyncLocal<HttpContext>
instance).This will obviously cause bugs that are really hard to track down.
The solution is to change the instance field to a class field:
The text was updated successfully, but these errors were encountered: