I need to run a heavy code parallel to other code so that user don't have to wait .. I am trying Task.Run(() =>
to accomplish my task. My task is calling method from service using dependency injection.
I am getting following error:
{"Resolution of the dependency failed, type = \"Company.CacheProvider.CacheEmployeeProvider\", name = \"(none)\".\r\nException occurred while: while resolving.\r\nException is: InvalidOperationException - The PerRequestLifetimeManager can only be used in the context of an HTTP request. Possible causes for this error are using the lifetime manager on a non-ASP.NET application, or using it in a thread that is not associated with the appropriate synchronization context.\r\n-----------------------------------------------\r\nAt the time of the exception, the container was:\r\n\r\n Resolving Company.Provider.CacheProvider.CacheEmployeeProvider,(none)\r\n Resolving parameter \"cache\" of constructor Company.Provider.CacheProvider.CacheEmployeeProvider(Company.Provider.Interface.IEmployeeProvider provider, Company.Provider.Interface.ICache cache)\r\n Resolving Company.Provider.CacheProvider.RequestCache,(none) (mapped from Company.Provider.Interface.ICache, (none))\r\n"}
Error is providing the possible reason .. but I am not able to find the solution ..
Entire application is using PerRequestLifetimeManager
.. how can I use it in may task .. or how can I make my task run in different context without any issue ..
UPDATE : - Registration
var container = new UnityContainer();
container.RegisterType<IEmployeeProvider>(new InjectionFactory(unity => unity.Resolve<CacheEmployeeProvider>(new DependencyOverride(typeof(IEmployeeProvider), unity.Resolve<DbEmployeeProvider>()))));
container.RegisterType<IEmployeeService, EmployeeService>();
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
DynamicModuleUtility.RegisterModule(typeof(Microsoft.Practices.Unity.Mvc.UnityPerRequestHttpModule));
Here is the base code of EmployeeController
Dependency
[Microsoft.Practices.Unity.Dependency]
public Lazy<IEmployeeService> EmployeeService { get; set; }
Post Request
[HttpPost]
public ActionResult AddEmployee(EmployeeModel model)
{
var employee = EmployeeService.Value.Add(model);
Task.Run(() => SetupEmployeeArea(employee.Id)); //task to setup employee related data
return RedirectToAction("EmployeeList"); //redirect user to the list of employees
}
Task
async Task SetupEmployeeArea(int id)
{
EmployeeService.Value.EmployeeSetup(id); //this call firing exception
}
I also tried following , but this code is not executing completely .. while debugging I lost the pointer in middle and nothing happening.
async Task SetupEmployeeArea(int id)
{
var employeeService = DependencyResolver.Current.GetService<IEmployeeService>();
employeeService.EmployeeSetup(id); //not getting any exception and code is also not running
}
Please share suggestions ... or if possible please also share best alternates to fulfill my requirement (run heavy code parallel) except "Hangfire" .. I already have too much load on that :)