I'm building an ASP.NET WebApi 2.1 app that needs an equivalent of HttpContext.Items as a per request cache.
I cannot use HttpContext even under IIS hosting because HttpContext seems to be lost when i'm doing async work (using TPL calls, not async/await due to some interface that needs to be matched) in services/repos layers (HttpContext.Current becomes null).
I'm using unity 3.5 and cannot achieve to do a proper per request injection. Tried the HttpControllerActivator method :
public class HttpControllerActivator : IHttpControllerActivator
{
private readonly IUnityContainer _container;
private readonly IHttpControllerActivator _activator;
public HttpControllerActivator(IUnityContainer container, IHttpControllerActivator activator)
{
_container = container;
_activator = activator;
}
public IHttpController Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
{
IHttpController controller = _activator.Create(request, controllerDescriptor, controllerType);
_container.RegisterInstance<System.Net.Http.HttpRequestMessage>(request, new HierarchicalLifetimeManager());
return controller;
}
}
But this register the HttpRequestMessage on the root container, not the child one created by the BeginScope() call inside _activator.Create. As a result, I'm getting mixed requests instances under concurrent load.
Any idea how to solves this ? I'm searching online since two days and haven't find any real solution to that matter...