Reputation: 35
I'm trying to initiate a Request/Response within a Consumer.Consume method and am struggling to populate the bus parameter.
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});
}
}
Ignoring the arbitrary example, is there any mechanism to get a reference to an instance of IBusControl to pass across to the MessageRequestClient constructor?
Upvotes: 0
Views: 668
Reputation: 33278
I would suggest the following steps to get to a happy place with your code.
First, update your class to accept the IBus
argument:
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});
}
}
Next, update your consumer registration to include the factory method to create the consumer:
IBusControl busControl = null;
busControl = Bus.Factory.CreateUsingInMemory(x =>
{
x.ReceiveEndpoint("my_service", e =>
{
e.Consumer(() => new DeleteUserConsumer(busControl));
})
})
The closure should capture a reference to the variable, and it should work properly to pass in the argument. Then, it should work!
Upvotes: 1