如何获得发布者的活动回复?

时间:2017-04-19 13:53:47

标签: c# rabbitmq microservices masstransit

我在c#中使用MassTransit和rabbitMQ。

我向消费者发送命令,让他们进入消费者并执行所需的任务,并尝试向发布者发送回复。

using MyCompany.Messaging;
using System;
using System.Threading.Tasks;

namespace MassTransit.Receiver
{
    public class RegisterCustomerConsumer : IConsumer<IRegisterCustomer>
    {       
        public Task Consume(ConsumeContext<IRegisterCustomer> context)
        {           
            IRegisterCustomer newCustomer = context.Message;
            Console.WriteLine("A new customer has signed up, it's time to register it in the command receiver. Details: ");
            Console.WriteLine(newCustomer.Address);
            Console.WriteLine(newCustomer.Name);
            Console.WriteLine(newCustomer.Id);
            Console.WriteLine(newCustomer.Preferred);

            context.Publish<ICustomerRegistered>(new
            {
                Address = newCustomer.Address,
                Id = newCustomer.Id,                
                RegisteredUtc = newCustomer.RegisteredUtc,
                Name = newCustomer.Name             
            });

            return Task.FromResult(context.Message);
        }
    }
}

我找到了这个正确获取消息并执行相关任务的示例代码。 主题的作者添加了这条评论:

Note that we didn’t have to specify a queue name here as opposed to sending a command to a single queue. We’ll see that the queue names are only provided in the consumers. MassTransit will create the necessary queues in the background.

现在,发布的响应消息将在何处以及如何在发布者中获得此响应?

由于

2 个答案:

答案 0 :(得分:1)

您可以在Github repository

中查看整个请求/回复示例

要使用请求/响应,您需要使用documentation

中所述的请求客户端

在请求使用者身上,您需要使用setSupportActionBar(toolbar); if (getSupportActionBar() != null) { getSupportActionBar().setDisplayShowTitleEnabled(false); title.setText("Payment"); } 将响应发送到您的请求客户端。

因此请求/响应要求您使用接收方地址。不支持发布请求,因为它没有意义,您只需要获得一个响应,而不是未知数量的响应。由于同样的原因,响应也会发送给请求者,而不是发布。

答案 1 :(得分:1)

如其他答案中所述,请查看请求/响应示例。

另外,请查看请求客户端用法的文档:

http://masstransit-project.com/MassTransit/usage/request-response.html