Skip to content

HttpContext is lost when executing async methods #110

Open
@ArnaudB88

Description

@ArnaudB88

I want to cache the result of a method with LazyCache.
That method uses other classes which are instantiated with dependency injection. Instances of that class are created with a lifetime manager using the HTTPContext.
In short, the 'addItemFactory' method which I want to cache the result of, needs the HttpContext.

var result = appCache.GetOrAddAsync("foo", async () => {
await Task.Delay(1000);
//Here a method is executed which need the HttpContext, can be seen as:
return System.Web.HttpContext.Current.Request;
}

It seems that when the execution is started of the async method, the HTTP context is lost.
The point where it loses the context is the contructor:

LazyCache.AsyncLazy<T>.AsyncLasy(taskFactory) :
base(() => Task.Factory.StartNew(taskFactory).Unwrap())

When starting a new task (Factory.StartNew()), the context is lost.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions