2017-04-19 63 views
2

我在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.

現在,這裏將發表響應消息,以及如何獲得出版商對此有何反應?

感謝

+1

貌似出版商應該訂閱'ICustomerRegistered'事件。 –

回答

1

您可以檢查Github repository整個請求/響應樣本。

要使用請求/響應,您需要使用請求客戶端,如documentation中所述。

在請求消費者上,您需要使用context.Respond(...)將響應發送給您的請求客戶端。

因此,請求/響應要求您使用接收者地址。由於發佈請求沒有意義,因此不支持發佈請求,只需要獲得一個響應,而不是未知數量的響應。出於同樣的原因,該響應也發送給請求者,而不是出版物。