【问题标题】:How to get response of event in publisher?如何在发布者中获得事件的响应?
【发布时间】:2017-09-15 19:18:57
【问题描述】:

我在 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.

现在,将在哪里发布响应消息以及如何在发布者中获取此响应?

谢谢

【问题讨论】:

  • 看起来发布者应该订阅ICustomerRegistered 事件。

标签: c# rabbitmq microservices masstransit


【解决方案1】:

如另一个答案中所述,请查看请求/响应示例。

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

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

【讨论】:

  • 克里斯,我也链接到同一页面。我相信那里缺少Respond 调用,我会添加它。
【解决方案2】:

您可以在Github repository 中查看整个请求/响应示例。

要使用请求/响应,您需要使用documentation 中所述的请求客户端。

在请求消费者上,您需要使用context.Respond(...) 将响应发送到您的请求客户端。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-29
    相关资源
    最近更新 更多