问题
I'm current writing a Windows Service which connects to an existing service layer using Entity Framework (DbContext), and Ninject to inject the Respositories and DbContext instance. This is pretty much working with one caveat - I want a brand new DbContext instance each time the thread runs, whereas at the moment I'm getting the same one for the entire thread lifetime.
My binding looks like this:
Bind<IDbContext>().To<EnterpriseDbContext>().InThreadScope();
Bind<IUserRepository>().To<UserRepository>().InThreadScope();
// And other repositories
And my thread code looks like this:
[Inject]
public IDbContext DbContext { get; set; }
// Execute indefinitely (or until we've stopped)
while (true && !isStopping)
{
try
{
// Do work.
// Save any changes.
DbContext.SaveAnyChanges();
}
catch (Exception ex)
{
// Handle exception
HandleException(ex);
}
// Sleep
Thread.Sleep(sleepInterval);
}
Now I know I can change the scope to InTransientScope(), etc - however I'm still fairly new to Ninject, and I'm not really sure how best to organise the code to use a new DbContext instance each time.
Has anyone ever done anything similar? In the web app, we have InRequestScope() which works perfectly - but I'm not really sure of the best approach here on how to use the DbContext in the Windows Service.
回答1:
See here for the answer
In Ninject2, you can do this by:
Bind<IService>().To<ServiceImpl>().InScope(ctx => ...);
The object returned by the callback passed to InScope()
becomes the "owning" object of instances activated within the scope. This has two meanings:
If the callback returns the same object for more than one activation, Ninject will re-use the instance from the first activation.
When the object returned from the callback is garbage collected, Ninject will deactivate ("tear down", call Dispose(), etc.) any instances associated with that object.
For example, the callback used for InRequestScope()
is:
ctx => HttpContext.Current
Since HttpContext.Current
is set to a new instance of HttpContext
on each web request, only a single instance of the service will be activated for each request, and when the request ends and the HttpContext
is (eventually) collected, the instances will be deactivated.
The object returned by the callback can also implement INotifyWhenDisposed
, an interface from Ninject, if you want to deterministically deactivate the "owned" instances. If the scoping object implements this interface, when it is Dispose()'d, any instances it owns will be deactivated immediately.
来源:https://stackoverflow.com/questions/18655915/ninject-dbcontext-windows-service-new-instance-each-time-thread-runs