Detect whether I am running from an HttpContext of a ThreadContext

35 views Asked by At

With StructureMap, I am registering an interface as follow:

public class PersistenceRegistry : Registry
{
    public PersistenceRegistry()
    {
        For<IClearableSessionProvider>().HybridHttpOrThreadLocalScoped().Use<FirebirdSessionProvider>();   
    }
}

I would like to know if it is possible to detect by any ways if the resolved instance is resolved from an HttpContext or a "ThreadContext". I don't even need to have access to the context. I just want to know whether I am in the context of an HttpRequest or from a background thread.

I tried several things consisting of injecting the HttpContext if it exists, but whatever I tried, I always get a null reference.

I tried to register an IHttpContextProvider:

public interface IHttpProvider
{
    HttpContext GetHttpContext();
}

public class HttpProvider
{
    HttpContext GetHttpContext()
    {
        return HttpContext.Current;
    }
}

But HttpContext.Current is always null (even once I get into the controller call).

I tried to register an HttpContextWrapper but also always null:

For<HttpContextBase>().Use(() => new HttpContextWrapper(HttpContext.Current))

I am using StructureMap-2.6.3.0. I am not sure how to formulate correctly but the app is built upon OWIN (which explains why my attempts always return null if I understand correctly what I have been reading before coming here).

0

There are 0 answers