1)嗨。我正在使用RabbitMQ学习MassTransit,但坚持使用Request / Respond。我阅读了很多文章,并尝试使用MassTransit文档编写控制台应用程序。但仍然无法找到有关使用IRequestClient接口初始化使用者的任何信息。这是我的代码:
static void Main(string[] args){
var serviceAddress = new Uri("loopback://localhost/notification.service");
var requestTimeout = TimeSpan.FromSeconds(120);
var bus = BusConfigurator.ConfigureBus((cfg, host) =>
{
cfg.ReceiveEndpoint(host, RabbitMqConstants.NotificationServiceQueue, e =>
{
e.Consumer(() => new OrderRegisteredConsumer(???));
});
});
IRequestClient<ISimpleRequest, ISimpleResponse> client = new MessageRequestClient<ISimpleRequest, ISimpleResponse>(bus, serviceAddress, requestTimeout);
bus.Start();
Console.WriteLine("Listening for Order registered events.. Press enter to exit");
Console.ReadLine();
bus.Stop();
}
我的消费者
public class OrderRegisteredConsumer: IConsumer<IOrderRegisteredEvent>
{
private static IBusControl _bus;
IRequestClient<ISimpleRequest, ISimpleResponse> _client;
public OrderRegisteredConsumer(IRequestClient<ISimpleRequest, ISimpleResponse> client)
{
_client = client;
}
public async Task Consume(ConsumeContext<IOrderRegisteredEvent> context)
{
await Console.Out.WriteLineAsync($"Customer notification sent: Order id {context.Message.OrderId}");
ISimpleResponse response = await _client.Request(new SimpleRequest(context.Message.OrderId.ToString()));
Console.WriteLine("Customer Name: {0}", response.CustomerName);
}
}
如何将我的客户端放入
e.Consumer(() => new OrderRegisteredConsumer(???));
2)我也尝试在传奇中找到关于请求/回应的一些信息,但不幸的是,我找到的只有https://github.com/MassTransit/MassTransit/issues/664
我会很感激如果某人有一个在传奇中使用它的例子,或者如果有人可以提供一些链接,我可以在那里阅读更多。
答案 0 :(得分:1)
您需要客户端变量可用,但客户端在配置端点时不需要准备就绪。 endpoint.Consumer
不会立即实例化消费者,它只需要一个工厂代理,当消息来自此消费者时,它将实例化消费者。
由于委托是引用类型,您可以稍后在代码中分配它。
所以这会奏效:
IRequestClient<ISimpleRequest, ISimpleResponse> client;
var bus = BusConfigurator.ConfigureBus((cfg, host) =>
{
cfg.ReceiveEndpoint(host, RabbitMqConstants.NotificationServiceQueue, e =>
{
e.Consumer(() => new OrderRegisteredConsumer(client));
});
});
client = new MessageRequestClient<ISimpleRequest, ISimpleResponse>(
bus, serviceAddress, requestTimeout);