In ASP.NET Core, I am using IHttpContextAccessor
to access the current HttpContext
. HttpContextAccessor
uses AsyncLocal<T>
.
In one situation, I am trying to start a Task
from within a request, that should continue running after the request has ended (long running, background task), and I am trying to detach it from the current execution context so that IHttpContextAccessor
returns null
(otherwise I am accessing an invalid HttpContext
).
I tried Task.Run
, Thread.Start
, but every time, the context seems to carry over and IHttpContextAccessor
returns the old context whatever I try (sometimes even contexts from other requests ).
How can I start a task that will have a clean AsyncLocal
state so that IHttpContextAccessor
returns null
?