在NServiceBus下在温莎管理RavenDb会话 [英] Managing RavenDb session in Windsor under NServiceBus

查看:84
本文介绍了在NServiceBus下在温莎管理RavenDb会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在MVC 4项目中使用NServiceBus(3.2.2),RavenDB(1.2.2017-不稳定)和Windsor(3.0.0.4001)。

I'm using NServiceBus (3.2.2), RavenDB (1.2.2017-Unstable) and Windsor (3.0.0.4001) in an MVC 4 project.

我有一个处理3种不同消息的IHandleMessages类,并且需要一个IDocumentSession,因此定义了一个属性,例如:

I have a IHandleMessages class that handles 3 different messages, and that needs an IDocumentSession, and therefore defines a property such as:

public IDocumentSession DocumentSession { get; set; }

我已经从NServiceBus的网站

I've copied the RavenDbUnitOfWork implementation from NServiceBus' website

我已经在我的Windsor容器中注册了IDocumentStore,IDocumentSession和IManageUnitsOfWork:

I've registered IDocumentStore, IDocumentSession and IManageUnitsOfWork in my Windsor container as follow:

container.Register(
            Component
                .For<IManageUnitsOfWork>()
                .ImplementedBy<RavenUnitOfWork>()
                .LifestyleTransient()
            );
container.Register(
            Component
                .For<IDocumentStore>()
                .UsingFactoryMethod(k => DocumentStoreHolder.DocumentStore)
                .LifestyleSingleton(),
            Component
                .For<IDocumentSession>()
                .UsingFactoryMethod(k => k.Resolve<IDocumentStore>().OpenSession())
                .LifestyleTransient()
            );

NServiceBus配置为使用我的容器:

NServiceBus is configured to use my container:

Configure.With()
         .CastleWindsorBuilder(container);

我遇到了一个问题,即UnitOfWork和消息处理程序接收到DocumentSession的不同实例。这意味着,由于在另一个DocumentSession上调用了SaveChanges(),因此不会保存存储在消息处理程序中的会话中的对象。

I'm encountering the problem that the UnitOfWork and the message handler receive different instances of the DocumentSession. This means that objects stored in the session in the message handler are not saved, since SaveChanges() is called on a different DocumentSession.

删除Transient生活方式会导致不同类型的问题,导致从RavenDb更新对象时导致并发性/冲突,因为(可能)消息处理程序不断获取相同的DocumentSession实例,该实例保存更新对象的缓存版本。

Removing the Transient lifestyle causes different kind of problems, that result in concurrency/conflicts when updating objects from RavenDb, since (probably) the message handler keeps getting the same instance of the DocumentSession, which holds a cached version of the updated object.

更新:

根据建议,我尝试将IDocumentSession在温莎的注册更改为Scope生活方式,像这样:

As suggested, I've tried changing the registration of the IDocumentSession in Windsor, to the Scope lifestyle, like this:

Component
    .For<IDocumentSession>()
    .UsingFactoryMethod(k => k.Resolve<IDocumentStore>().OpenSession())
    .LifestyleScope()

当容器尝试解析MVC控制器时,这会导致异常,并指出未找到范围,并询问我是否忘记了c全部BeginScope()。

This causes exceptions when the container tries to resolve the MVC Controller, saying that the scope was not found, and asking if I forgot to call BeginScope().

推荐答案

您需要具有按消息范围,而不是瞬态或单例范围。

You need to have a scope of Per Message, not transient or singleton.

这篇关于在NServiceBus下在温莎管理RavenDb会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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