Ninject,的DbContext和放大器; Windows服务 - 新的实例时线程运行? [英] Ninject, DbContext & Windows Service - New Instance Each Time Thread Runs?

查看:264
本文介绍了Ninject,的DbContext和放大器; Windows服务 - 新的实例时线程运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前写一个Windows服务,连接到使用实体框架(的DbContext),并Ninject注入的Respositories和的DbContext例如现有的服务层。这是pretty的更多的工作有一点需要注意 - 我想,每次一个全新的DbContext实例的线程运行,而此刻我得到的同一个整个线程生命周期

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

和我的线程code是这样的:

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);
 }

现在我知道我可以改变的范围,以InTransientScope(),等等 - 但是我还是相当新的Ninject,我真的不知道如何以最佳方式组织code使用一个新的DbContext实例每一次。

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.

有没有人做过类似的话?在Web应用程序中,我们有InRequestScope(),它完美的作品 - 但我真的不知道这里的​​最佳方法如何使用的DbContext在Windows服务。

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.

推荐答案

看这里的回答

在Ninject2,你可以这样做:

In Ninject2, you can do this by:

Bind<IService>().To<ServiceImpl>().InScope(ctx => ...);

InScope()由传递给回调返回的对象变成范围内激活的情况下的拥有的对象。这有两方面的含义:

The object returned by the callback passed to InScope() becomes the "owning" object of instances activated within the scope. This has two meanings:

  1. 如果回调返回多于一个激活的同一个对象,Ninject将从第一激活重新使用该实例

  1. If the callback returns the same object for more than one activation, Ninject will re-use the instance from the first activation.

当物体从回调返回的垃圾回收,Ninject将停用(推倒,调用Dispose()等)与该对象相关联的所有实例。

When the object returned from the callback is garbage collected, Ninject will deactivate ("tear down", call Dispose(), etc.) any instances associated with that object.

例如,使用回调InRequestScope()是:

ctx => HttpContext.Current

由于 HttpContext.Current 设置为的HttpContext 一个新实例上的每个Web请求,只有一个实例服务的每一个请求将被激活,并在请求结束并且当 HttpContext的是(最终)收集,该实例将被停用。

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.

由回调返回的对象还可以实现 INotifyWhenDisposed ,接口从Ninject,如果你想要确定性停用拥有的实例。如果作用域的对象实现了这个接口,当它的Dispose()处理,它拥有的任何实例将被立即取消。

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.

这篇关于Ninject,的DbContext和放大器; Windows服务 - 新的实例时线程运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆