2016-08-09 62 views
0

我試圖在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構造?

回答

1

我會建議以下步驟,以獲得與您的代碼快樂的地方。

首先,更新你的類接受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)); 
    }) 
}) 

的截止時間應捕獲引用該變量,並且它應該正確地傳遞參數。那麼,它應該工作!

相關問題