如何使用asp.net同一DI code和窗口服务 [英] How to use the same DI code in asp.net and a windows service

查看:133
本文介绍了如何使用asp.net同一DI code和窗口服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于如何使用相同的DI code在ASP.NET MVC应用程序和窗口服务既使用NHibernate的相同的数据库进行交互的问题。然而,Windows服务执行后台任务。

I have a question about how to use the same DI code in an ASP.NET mvc app and a windows service as both interact with the same database using NHibernate. The windows service however performs background tasks.

像其他人我有一个新的控制器工厂,知道如何使用StructureMap实例化控制器。因此,我可以给我的控制器,它们的构造函数的参数要求的存储库。由于Structuremap使用HTTPContextScope控制的ISession生命周期的话,我可以肯定的是所有存储库收到的ISession的同一个实例为每个web请求。

Like everyone I have a new controller factory that knows how to instantiate controllers using StructureMap. Thus I can have the required repositories given to my controllers as parameters of their constructors. Since Structuremap controls the ISession life cycle using the HTTPContextScope then I can be sure that all repositories receive the same instance of the ISession for every web request.

然而,在一个窗口服务,并使用Structuremap(同DI code)我不知道一个很好的方式,以逻辑隔离不同的背景任务,以自己独特的ISession实例,并与他们所使用的资料库共享。我想有同时执行多个任务,因此ISession的不能是一个单独的对象(如推荐使用NHibernate的桌面应用程序)。

However in a windows service and using Structuremap (the same DI code) I don't know a nice way to logically isolate different background tasks to own a unique ISession instance and share it with the repositories they use. I want to have multiple tasks executing simultaneously, therefore the ISession can not be a singleton object (as recommended for desktop apps using NHibernate).

我要preserve并介绍每请求模式一个会在我的窗口服务信任Structuremap给我们每个任务相同的ISession实例。任何人都可以点我朝着正确的方向吗?

I want to preserve and introduce the One Session Per Request pattern in my windows service trusting Structuremap to give us the same ISession instance per Task. Can anyone point me towards the right direction?

感谢

这两个部分的职位由科里·库根是非常沿着我觉得这个问题应该是线解决。不过我不熟悉WCF和倾向于是其部分在文章中混淆。

This two part post by Corey Coogan is very much along the lines I think this problem should be resolved. However I'm not familiar with WCF and tend to get confused by its parts in the article.

http://blog.coreycoogan.com / 2010/5月26日/ structuremap-WCF-NHibernate的部分-1 /
http://blog.coreycoogan.com/2010/ 5月27日/ structuremap-WCF-NHibernate的部分-2 /

推荐答案

在我的Windows服务我有一个引导程序是完全一样的一个在ASP.NET MVC:

In my Windows Service I have a bootstrapper which is exactly the same as the one in ASP.NET MVC:

public static class Bootstrapper
{
    public static void Initialize()
    {
        StructureMap.ObjectFactory.Initialize(cfg =>
        {
            cfg.UseDefaultStructureMapConfigFile = false;
            cfg.IgnoreStructureMapConfig = true;
            cfg.AddRegistry<Web.Core.DI.NhibernateRegistry>();
            cfg.AddRegistry<Web.Core.DI.DomainRegistry>();
            cfg.AddRegistry<Web.Core.DI.ServicesRegistry>();
            cfg.AddRegistry<Web.Core.DI.QuartzRegistry>();
        });
        ServiceLocator.SetLocatorProvider(() => new StructureMapServiceLocator(ObjectFactory.Container));
    }
}

然后我定义每个模块的注册表:

then I have defined a registry for each module:

public class NhibernateRegistry: Registry
{
    public NhibernateRegistry()
    {
        SchemaOperation schemaOperation = SchemaOperation.None; 

        For<ISessionFactory>()
            .Singleton()
            .Use(new BpReminders.Data.NH.NHibernateSessionFactory(connectionString, schemaOperation).SessionFactory);

        For<IUnitOfWork>()
            .HybridHttpOrThreadLocalScoped()
            .Use<BpReminders.Data.NH.UnitOfWork>();

        For<ISession>()
            .HybridHttpOrThreadLocalScoped()
            .Use(o => ((BpReminders.Data.NH.UnitOfWork)o.GetInstance<IUnitOfWork>()).CurrentSession);
    }
}

我尽快引导一切作为服务启动:

I bootstrap everything as soon as the service starts:

protected override void OnStart(string[] args)
{
    Bootstrapper.Initialize();
}

最后我采取了Singleton模式引起我的Windows服务中不使用任何额外的线程。
我知道,StructureMap得到了这可能是你想要做什么有用的关键字AlwaysUnique:

At the end I adopted a Singleton pattern cause my Windows Service doesn't use any extra threads. I know that StructureMap has got a keyword AlwaysUnique which might be useful for what you're trying to do:

For<ISession>()
    .AlwaysUnique()

在code评论说(约AlwaysUnique)

the code comments say (about AlwaysUnique)

///部队StructureMap始终使用一个唯一的实例,以结果
///停止BuildSession缓存

/// Forces StructureMap to always use a unique instance to
/// stop the "BuildSession" caching

您在这个事业我刚刚做了几个测试挖。没有多少。

You have to dig in this cause I've just done a couple of test. Not much.

这篇关于如何使用asp.net同一DI code和窗口服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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