使用MassTransit时的工作单位 [英] Unit of work when using MassTransit

查看:72
本文介绍了使用MassTransit时的工作单位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种挂接消息处理管道并在使用者完成处理某些消息后做一些工作的方法. 我的意图是打开一个新会话并开始一个事务(可以在IoC容器中完成),然后再处理和处置它们.

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.

在NServiceBus中,我将使用IMessageModule接口进行挂接.有没有类似的东西?实际上,部署处理程序也可以做到这一点,但是当我将StructureMap用作ObjectBuilder时,Release方法什么也没做.

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.

推荐答案

您可以注册在使用每条消息之前和之后要调用的拦截器.例如:

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

查看MassTransit.Tests项目中的MessageInterceptor_Specs.cs文件,以进行工作单元测试.

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

这篇关于使用MassTransit时的工作单位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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