简体   繁体   English

MassTransit交易发布

[英]MassTransit transactional publishing

I can't figure out how I can publish a message transactional using MassTransit 3.1.2 and RabbitMQ 3.6.0. 我无法弄清楚如何使用MassTransit 3.1.2和RabbitMQ 3.6.0发布消息事务。 I have a basic scenario where I save something to the database and then I want to publish an event. 我有一个基本的场景,我将东西保存到数据库,然后我想发布一个事件。 Wrapping a System.Transactions.TransactionScope around it does not work, before I complete the scope the message is already sent: 在我完成范围之前包装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();
}

The bus is created as follows: 总线创建如下:

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"]);
    });
});

RabbitMQ does not enroll in transactions. RabbitMQ不会注册交易。 This is not possible. 这是不可能的。

I know its been a while for this, and I might not have understood the question correctly but if you want to raise the event after the database transaction is done you can probably do something like this 我知道它已经有一段时间了,我可能没有正确理解这个问题,但如果你想在数据库事务完成后提出事件,你可能会做这样的事情

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM