MassTransit交易发布

时间:2016-02-10 22:46:30

标签: c# .net transactions rabbitmq masstransit

我无法弄清楚如何使用MassTransit 3.1.2和RabbitMQ 3.6.0发布消息事务。我有一个基本的场景,我将东西保存到数据库,然后我想发布一个事件。在我完成范围之前包装System.Transactions.TransactionScope不起作用,消息已经发送:

using (var scope = new TransactionScope(TransactionScopeOption.Required))
{
     // Save to database here.

     bus.Publish(new EntityCreatedEvent());

     // Event is already published at this point.

     scope.Complete();
}

总线创建如下:

var rabbitMqHostAddress = new Uri($"rabbitmq://{ConfigurationManager.AppSettings["RabbitMqHost"]}/");

var bus = Bus.Factory.CreateUsingRabbitMq(r =>
{
    r.Host(rabbitMqHostAddress, h =>
    {
        h.Username(ConfigurationManager.AppSettings["RabbitMqUsername"]);
        h.Password(ConfigurationManager.AppSettings["RabbitMqPassword"]);
    });
});

2 个答案:

答案 0 :(得分:0)

RabbitMQ不会注册交易。这是不可能的。

答案 1 :(得分:0)

我知道这已经有一段时间了,我可能没有正确理解这个问题,但是如果您想在数据库事务完成后引发该事件,则可以执行以下操作

using (var scope = new TransactionScope(TransactionScopeOption.Required))
{
 // Save to database here.

scope.TransactionCompleted += (sender, args) => { bus.Publish(new EntityCreatedEvent()); };

 // Event is already published at this point.

 scope.Complete();
}