如何在发布者中获得事件响应?
How to get response of event in publisher?
我在 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.
现在,发布响应消息的位置以及如何在发布者中获取此响应?
谢谢
您可以在 Github repository 中检查整个 request/response 样本。
要使用 request/response,您需要使用 documentation 中所述的请求客户端。
在请求消费者上,您需要使用context.Respond(...)
将响应发送给您的请求客户端。
因此 request/response 要求您使用收件人地址。不支持发布请求,因为它没有意义,您只需要获得一个响应,而不是未知数量的响应。出于同样的原因,响应也发送给请求者,而不是发布。
如其他答案中所述,请查看 request/response 示例。
此外,查看请求客户端用法的文档:
http://masstransit-project.com/MassTransit/usage/request-response.html
我在 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.
现在,发布响应消息的位置以及如何在发布者中获取此响应?
谢谢
您可以在 Github repository 中检查整个 request/response 样本。
要使用 request/response,您需要使用 documentation 中所述的请求客户端。
在请求消费者上,您需要使用context.Respond(...)
将响应发送给您的请求客户端。
因此 request/response 要求您使用收件人地址。不支持发布请求,因为它没有意义,您只需要获得一个响应,而不是未知数量的响应。出于同样的原因,响应也发送给请求者,而不是发布。
如其他答案中所述,请查看 request/response 示例。
此外,查看请求客户端用法的文档:
http://masstransit-project.com/MassTransit/usage/request-response.html