Unit of work when using MassTransit

浪子不回头ぞ 提交于 2019-12-05 01:29:40

问题


I'm looking for a way to hook on the message handling pipeline and do some work after a consumer finishes handling some message. My intention is to open a new session and start a transaction(could be done the IoC Container) before handling and disposing them right after it.

In NServiceBus I would use the IMessageModule interface to hook in. does have anything similar to it? Actually disposing the the handler would also do it for, but as I'm using StructureMap as the ObjectBuilder, the Release method just does nothing.


回答1:


You can register an interceptor to be called before and after each message is consumed. As an example:

        LocalBus = ServiceBusConfigurator.New(x =>
            {
                x.ReceiveFrom("loopback://localhost/mt_client");

                x.BeforeConsumingMessage(() => { _before.Set(); });
                x.AfterConsumingMessage(() => { _after.Set(); });
            });

Take a look at the MessageInterceptor_Specs.cs file in the MassTransit.Tests project for a working unit test.




回答2:


I went through the same challenge and this is how I went about it. We had an IUnitOfWork and an ITransaction: with Commit() and Rollback() we added a class for TransactionalOperation : ITransaction to added support transactional behaviour to non-db stuff. The IUnitOfWork.Commit() iterates through a list of TransactionalOperations that might have been added to it.

Now to tie the bus to our system: Added an IBus to wrap external bus Implemented a MassTransitBusGateway: IBus then to tie the bus to unit of work: Implemented UnitOfWorkBus: IBus (decorator) - this decorator makes any calls to Publish() to be aware of the unit of work and add it as TransactionalOperation so that Its execution is delayed until the UnitOfWork.Commit()

This way we are abstracting the concrete bus, thus avoiding adding MassTransit dependencies on several projects, (only the clients really needs it) and added transactional behavior to its operations.



来源:https://stackoverflow.com/questions/5569320/unit-of-work-when-using-masstransit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!