Im writing a plugin for nopCommenre and encountered the following problem: one of the controller's action method should run a height-load operation in background thread. nopCommerce uses Autofac as IoC container.
If I understand correctly, I need to create new lifescope dependent on old one to use Autofac in background thread. I found the following solution, but it does not work:
public void Run<T>(Action<T> action)
{
Task.Factory.StartNew(delegate
{
using (var container = AutofacDependencyResolver.Current.ApplicationContainer.BeginLifetimeScope("AutofacWebRequest"))
{
var service = container.Resolve<T>();
action(service);
}
});
}
I've got an error "The request lifetime scope cannot be created because the HttpContext is not available". This answer helps me to upderstand the reason of this behaviour, but I still dont know how to resolve services using Autofac in background thread. I cant change source code of nopCommerce, so I cant save reference to the original Autofac container as adviced in answer above.