在消费者中启动请求/响应模式
我试图在Consumer.Consume方法中启动请求/响应,并且正努力填充总线参数。在消费者中启动请求/响应模式
public class DeleteUserConsumer : IConsumer<IDeleteUser> {
public async Task Consume(ConsumeContext<IDeleteUser> context)
{
var address = "rabbitmq://host/vhost/queue"
var timeout = TimeSpan.FromSeconds(15);
var bus = context.??? // was hoping to get an IBusControl instance from the context - but perhaps this isn't possible?
IRequestClient<IGetUserDetails,IUserDetails> client = new MessageRequestClient<IGetUserDetails, IUserDetails>(bus, address, timeout);
var userDetails = await client.Request(new IGetUserDetails() {Name = context.Message.Name});
}
}
忽略任意例子,有没有任何机制去IBusControl的实例的引用跨传递给MessageRequestClient构造?
回答:
我会建议以下步骤,以获得与您的代码快乐的地方。
首先,更新你的类接受IBus
说法:
public class DeleteUserConsumer : IConsumer<IDeleteUser>
{
IBus _bus;
public DeleteUserConsumer(IBus bus)
{
_bus = bus;
}
public async Task Consume(ConsumeContext<IDeleteUser> context)
{
var address = "rabbitmq://host/vhost/queue"
var timeout = TimeSpan.FromSeconds(15);
IRequestClient<IGetUserDetails,IUserDetails> client = new MessageRequestClient<IGetUserDetails, IUserDetails>(_bus, address, timeout);
var userDetails = await client.Request(new IGetUserDetails() {Name = context.Message.Name});
}
}
下一步,更新你的消费者注册,包括工厂方法来创建消费者:
IBusControl busControl = null; busControl = Bus.Factory.CreateUsingInMemory(x =>
{
x.ReceiveEndpoint("my_service", e =>
{
e.Consumer(() => new DeleteUserConsumer(busControl));
})
})
的截止时间应捕获引用该变量,并且它应该正确地传递参数。那么,它应该工作!
以上是 在消费者中启动请求/响应模式 的全部内容, 来源链接: utcz.com/qa/257381.html