I am using ASP.NET MVC with Castle Windsor as my IoC container with the lifestyle component installed in PerWebRequest. My repository (which is dependent on it) creates an instance of the Entity Framework ObjectContext in the constructor, and I store it in a private instance variable. My repository implements IDisposable and inside my Dispose method, I delete the ObjectContext. I think all this is pretty standard, and here's a simplified illustration:
Repository:
public class Repository : IRepository { private MyContext _dc;
To make sure there is no memory leak and that my Dispose () repository is being called, I override the DefaultControllerFactory ReleaseController method to free the Windsor container:
public class WindsorControllerFactory : DefaultControllerFactory { IWindsorContainer _container; public WindsorControllerFactory(IWindsorContainer container) { _container = container;
I think all this is pretty standard. However, I would like to unscrew the parallel stream, and the IRepository dependency is used inside this parallel stream. My problem is that my repository will already be deleted by the time it is used:
public class HomeController : Controller { IRepository _repository; public HomeController(IRepository repository) { _repository = repository; } public ActionResult Index() { var c = _repository.GetCompany(34); new Task(() => { System.Threading.Thread.Sleep(2000);
How do other people solve this problem? How do you pass your dependencies to a parallel thread?
Thanks in advance.
source share