使用Structureer为NServiceBus和MVC管理RavenDB IDocumentSession生命周期

tke*_*ker 5 structuremap nservicebus ravendb asp.net-mvc-4

我在我们的解决方案中使用NServiceBus v4.3,MVC4,RavenDB 2.5和StructureMap 2.6.4.

我在StructureMap下遇到类似问题的问题,在这个问题回答中我要求MVC控制器使用不同的生命周期,NServiceBus Handler在我的Web项目中使用RavenDB的IDocumentSession.

特别是在我的情况下,如果我使用HybridHttpOrThreadLocalScoped(如上面的答案建议Windsor)生命周期,会话没有被妥善处理,我很快就达到了30个事务限制错误.如果我使用HttpContext生命周期,则不会调用Web项目中的NSB事件处理程序.

在我的控制器中,会话被包含在通过MVC ActionFilter应用的工作单元中.我也使用处理程序中的UoW,因为我的注册表已连线以从UoW检索会话.代码是这样的:

RavenDbWebRegistry.cs

public sealed class RavenDbWebRegistry : Registry
{
    public RavenDbWebRegistry()
    {
        // register RavenDB document store
        ForSingletonOf<IDocumentStore>().Use(() =>
        {
            var documentStore = new DocumentStore
            {
                ConnectionStringName = "RavenDB",
                Conventions =
                {
                    IdentityPartsSeparator = "-", 
                    JsonContractResolver = new PrivatePropertySetterResolver(),
                },

            };
            documentStore.Initialize();

            return documentStore;
        });


        For<IDocumentSession>().HybridHttpOrThreadLocalScoped().Add(ctx =>
        {
            var uow = (IRavenDbUnitOfWork)ctx.GetInstance<IUnitOfWork>();
            return uow.DocumentSession;
        });

        For<IUnitOfWork>().HybridHttpOrThreadLocalScoped().Use<WebRavenDbUnitOfWork>();            

    }
}
Run Code Online (Sandbox Code Playgroud)

Web项目处理程序示例:

public class SiteCreatedEventHandler : IHandleMessages<ISiteCreatedEvent>
{
    public IBus Bus { get; set; }
    public IUnitOfWork Uow { get; set; }
    public IDocumentSession DocumentSession { get; set; }

    public void Handle(ISiteCreatedEvent message)
    {
        try
        {
            Debug.Print(@"{0}{1}", message, Environment.NewLine);

            Uow.Begin();
            var site = DocumentSession.Load<Site>(message.SiteId);
            Uow.Commit();

            //invoke Hub and push update to screen
            var context = GlobalHost.ConnectionManager.GetHubContext<AlarmAndNotifyHub>();

            //TODO make sure this SignalR function is correct
            context.Clients.All.displayNewSite(site, message.CommandId);
            context.Clients.All.refreshSiteList();            
        }
        catch (Exception ex)
        {                
            Uow.Rollback();
        }            
    }
}
Run Code Online (Sandbox Code Playgroud)

ActionFilter的用法:

    [RavenDbUnitOfWork]
    public ViewResult CreateNew(int? id)
    {
        if (!id.HasValue || id.Value <= 0)
            return View(new SiteViewModel { Guid = Guid.NewGuid() });

        var targetSiteVm = MapSiteToSiteViewModel(SiteList(false)).FirstOrDefault(s => s.SiteId == id.Value);

        return View(targetSiteVm);
    }
Run Code Online (Sandbox Code Playgroud)

WebRegistry(在我的MVC项目中设置NSB)

public sealed class WebRegistry : Registry
{
    public WebRegistry()
    {
        Scan(x =>
        {
            x.TheCallingAssembly();
            x.Assembly("IS.CommonLibrary.ApplicationServices");
            x.LookForRegistries();
        });

        IncludeRegistry<RavenDbWebRegistry>();

        FillAllPropertiesOfType<IUnitOfWork>();
        FillAllPropertiesOfType<IDocumentSession>();
        FillAllPropertiesOfType<StatusConversionService>();
        FillAllPropertiesOfType<IStateRepository<TieState>>();
        FillAllPropertiesOfType<IStateRepository<DedState>>();
        FillAllPropertiesOfType<ITieService>();
        FillAllPropertiesOfType<IDedService>();
        FillAllPropertiesOfType<IHbwdService>();

        //NServiceBus
        ForSingletonOf<IBus>().Use(
        NServiceBus.Configure.With()
            .StructureMapBuilder()
            .DefiningCommandsAs(t => t.Namespace != null && t.Namespace.EndsWith("Command"))
            .DefiningEventsAs(t => t.Namespace != null && t.Namespace.EndsWith("Event"))
            .DefiningMessagesAs(t => t.Namespace == "Messages")
            .RavenPersistence("RavenDB")
            .UseTransport<ActiveMQ>()
            .DefineEndpointName("IS.Argus.Web")
            .PurgeOnStartup(true)
            .UnicastBus()
            .CreateBus()
            .Start(() => NServiceBus.Configure.Instance
            .ForInstallationOn<Windows>()
            .Install())
        );


        //Web             
        For<HttpContextBase>().Use(() => HttpContext.Current == null ? null : new HttpContextWrapper(HttpContext.Current));
        For<ModelBinderMappingDictionary>().Use(GetModelBinders());
        For<IModelBinderProvider>().Use<StructureMapModelBinderProvider>();
        For<IFilterProvider>().Use<StructureMapFilterProvider>();
        For<StatusConversionService>().Use<StatusConversionService>();
        For<ITieService>().Use<TieService>();
        For<IDedService>().Use<DedService>();
        For<IHbwdService>().Use<HbwdService>();
        For<ISiteService>().Use<SiteService>();

        IncludeRegistry<RedisRegistry>();
    }
Run Code Online (Sandbox Code Playgroud)

我已经尝试使用我能想到的每种可能的组合配置我的注册表无济于事.

鉴于StructureMap混合生命周期不能像我期望的那样工作,我必须做些什么来实现正确的行为?

UoW对RavenDB是否必要/有益?我喜欢它(从我之前的NHibernate UoW ActionFilter中调整过它),因为它管理Controller Actions中会话的生命周期,但我对其他方法持开放态度.

理想情况下,我想要的是 - 在Web项目中 - 为控制器和处理程序分配完全不同的IDocumentSessions,但是无法以任何方式解决这个问题.

Dom*_*icz 2

首先,RavenDB已经通过包装实现了工作单元IDocumentSession,所以不需要它。打开一个会话,调用SaveChanges()和处理就完成了工作单元

其次,控制器可以通过几种方式实现会话。

一般指导是在 中设立商店Global.asax.cs。由于只有 1 个框架实现了IDocumentSessionRavenDB,因此您不妨从Global. 如果它是存储库后面的 NHibernate 或实体框架,我会理解。但 IDocumentSession 是 RavenDB 特定的,因此请在Application_Start.

public class Global : HttpApplication
{
   public void Application_Start(object sender, EventArgs e)
   {
      // Usual MVC stuff

      // This is your Registry equivalent, so insert it into your Registry file 
      ObjectFactory.Initialize(x=> 
      {
         x.For<IDocumentStore>()
          .Singleton()
          .Use(new DocumentStore { /* params here */ }.Initialize());
   }

   public void Application_End(object sender, EventArgs e)
   {
      var store = ObjectFactory.GetInstance<IDocumentStore>();

      if(store!=null)
         store.Dispose();
   }
}
Run Code Online (Sandbox Code Playgroud)

在控制器中,添加一个基类,然后它可以为您打开和关闭会话。同样IDocumentSession是 RavenDB 特有的,所以依赖注入实际上并不能帮助你。

public abstract class ControllerBase : Controller
{
   protected IDocumentSession Session { get; private set; }

   protected override void OnActionExecuting(ActionExecutingContext context)
   {
      Session = ObjectFactory.GetInstance<IDocumentStore>().OpenSession();
   }

   protected override void OnActionExecuted(ActionExecutedContext context)
   {
      if(this.IsChildAction)
         return;

      if(content.Exception != null && Session != null)
         using(context)
            Session.SaveChanges();
   }
}
Run Code Online (Sandbox Code Playgroud)

然后从那里继承基本控制器并从那里开始工作:

public class CustomerController : ControllerBase
{
   public ActionResult Get(string id)
   {
      var customer = Session.Load<Customer>(id);

      return View(customer);
   }

   public ActionResult Edit(Customer c)
   {
      Session.Store(c);

      return RedirectToAction("Get", c.Id);
   }
 }
Run Code Online (Sandbox Code Playgroud)

最后,我可以看到您正在使用 StructureMap,因此只需进行一些基本调用即可从 DI 框架获取 Session:

public class SiteCreatedEventHandler : IHandleMessages<ISiteCreatedEvent>
{
    public IBus Bus { get; set; }
    public IUnitOfWork Uow { get; set; }
    public IDocumentSession DocumentSession { get; set; }

    public SiteCreatedEventHandler()
    {
       this.DocumentSession = ObjectFactory.GetInstance<IDocumentStore>().OpenSession();
    }

    public void Handle(ISiteCreatedEvent message)
    {
       using(DocumentSession)
       {
          try
          {
             Debug.Print(@"{0}{1}", message, Environment.NewLine);

             ///// Uow.Begin(); // Not needed for Load<T>

             var site = DocumentSession.Load<Site>(message.SiteId);

             //// Uow.Commit(); // Not needed for Load<T>

             // invoke Hub and push update to screen
             var context = GlobalHost.ConnectionManager.GetHubContext<AlarmAndNotifyHub>();

             // TODO make sure this SignalR function is correct
             context.Clients.All.displayNewSite(site, message.CommandId);
             context.Clients.All.refreshSiteList();            
         }
         catch (Exception ex)
         {                
             //// Uow.Rollback(); // Not needed for Load<T>
         }            
      }
    }
Run Code Online (Sandbox Code Playgroud)

  • 查询 IoC 容器是否一开始就否定了 DI 的好处?我可能错过了你的帖子的要点,但这些对“ObjectFactory.GetInstance”的调用看起来更像是[服务定位器](http://en.wikipedia.org/wiki/Service_locator_pattern)反模式。 (2认同)