2016-09-18 94 views
1

您好enybody知道如何解決MassTransit中的以下問題:消費者獲取請求和響應,但響應沒有返回到client.Request。方法。我已經在ASP NET WEB API創建的項目和我已經通過IRequestClient接口來實現請求/響應通信:MassTransit從消費者獲得響應

public class RequestResponseCommandProvider<TRequest, TResponse> 
    : IRequestResponseCommandProvider<TRequest, TResponse> 
    where TRequest : class, ICommandQueueName 
    where TResponse : class 
{ 
    private readonly IBusControl _bus; 
    private readonly string _hostUri; 
    public RequestResponseCommandProvider(IBusControl bus, 
     string hostUri) 
    { 
     _bus = bus; 
     _hostUri = hostUri; 
    } 

    public TResponse RequestResponseCommand(TRequest command) 
    { 
     _bus.Start(); 
     var serviceAddress = new Uri(_hostUri + command.QueueName); 
     IRequestClient<TRequest, TResponse> client = 
      _bus.CreateRequestClient<TRequest, TResponse>(serviceAddress, TimeSpan.FromSeconds(10)); 
     return client.Request(command).Result; //there should back response 
    } 
} 

我在Autofac創建serviceBus的結構模塊:

public class BusModule : Autofac.Module 
{ 
    private readonly string _hostUri; 
    IEnumerable<IConfigurableConsumer> _consumers; 

    public BusModule(string hostUri, IEnumerable<IConfigurableConsumer> consumers) 
    { 
     _hostUri = hostUri; 
     _consumers = consumers; 
    } 

    protected override void Load(ContainerBuilder builder) 
    { 
     builder.RegisterAssemblyTypes(AppDomain.CurrentDomain.GetAssemblies()); 

     builder.Register(r => Bus.Factory.CreateUsingRabbitMq(sfc => 
     { 
      var host = sfc.Host(new Uri(_hostUri), h => 
      { 
       h.Username("guest"); 
       h.Password("guest"); 
      }); 

      if (_consumers != null) 
      { 
       foreach (var consumer in _consumers) 
       { 
        consumer.Configure(sfc); 
       } 
      } 
     })) 
     .As<IBus>() 
     .As<IBusControl>() 
     .SingleInstance(); 

     builder.RegisterType<RecieveObserver>() 
      .As<IReceiveObserver>(); 
    } 
} 

消費者通過加入構造函數。 提供注入服務:

public class TestLayer : ITestLayer 
{ 
    private readonly IRequestResponseCommandProvider<IAddTestCommand, ResponseCommand> _provider; 
    public TestLayer(
     IRequestResponseCommandProvider<IAddTestCommand, ResponseCommand> provider) 
    { 
     _provider = provider; 
    } 
    public ServiceResult CreateTest(TestRecord record) 
    { 
     ServiceResult result; 
     try 
     { 
      var tmp = _provider.RequestResponseCommand(new AddTestCommand() { Record = "d3d32" }); 
      result = new ServiceResult(); 
     } 
     catch (Exception ex) 
     { 
      result = new ServiceResult(); 
      result.AddError($"Wystąpił problem podczas zapisu do bazy danych: {ex}"); 
     } 

     return result; 
    } 
} 

當我檢查隊列中RabbitMQ的所有郵件看起來像這樣的:由克里斯·帕特森 RabbitMQ queue

我已經看到採樣requestResponse則,但我有問題,當我使用依賴注入。 我會幫忙感謝我做錯了。還有GitHub上的所有庫,在這裏你可以找到簡單的項目中,該代碼包含仍然不工作:My GitHub

回答

0

兩個問題:

  1. 總線的懶惰實例並不是一個好主意,因爲它需要相當長的一段時間,在你的情況下,當IBus首次得到解決時,你會得到一個很長的超時時間。
  2. 您沒有收到任何回覆,因爲您需要啓動公交車才能接收任何內容。如果你不啓動公交車,你只能發送。