【发布时间】: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