2017-08-09 39 views
1

延長路由的配置我有一個畫謎配置項目這是許多Web API項目問:如何滷麪

所以基本上共享,它看起來像

的Web API 1 ==>共享Rebus的配置

網絡API 2 ==>共享Rebus的配置

的Web API 3 ==>共享滷麪配置

我的問題是,如果我有一些消息在網絡API 3項目&處理,我怎麼可以配置爲他們的路由?


我目前的配置:

var autofacContainerAdapter = new AutofacContainerAdapter(container); 

return Configure 
    .With(autofacContainerAdapter) 
    .Serialization(s => s.UseNewtonsoftJson()) 
    .Routing(r => 
    { 
     r.TypeBased() 
      .MapAssemblyOf<ProjectA.MessageA>(EnvironmentVariables.ServiceBusQueueName) 
      .MapAssemblyOf<ProjectB.MessageB>(EnvironmentVariables.ServiceBusQueueName); 
    }) 
    .Sagas(s => 
    { 
     s.StoreInSqlServer(EnvironmentVariables.ConnectionString, "Saga", "SagaIndex"); 
    }) 
    .Options(o => 
    { 
     o.LogPipeline(); 
     o.EnableDataBus().StoreInBlobStorage(EnvironmentVariables.AzureStorageConnectionString, EnvironmentVariables.BlobStorageContainerName); 
     o.EnableSagaAuditing().StoreInSqlServer(EnvironmentVariables.ConnectionString, "Snapshots"); 
    }) 
    .Logging(l => 
    { 
     l.Use(new SentryLogFactory()); 
    }) 
    .Transport(t => 
    { 
     t.UseAzureServiceBus(EnvironmentVariables.AzureServiceBusConnectionString, EnvironmentVariables.ServiceBusQueueName).AutomaticallyRenewPeekLock(); 
    }) 
    .Start(); 
+0

是什麼阻止您爲第三個Web API配置路由,方法與第一個相同? – mookid8000

+0

@ mookid8000我有一些消息和處理程序,只適用於第三個網頁api –

回答

1

嗯......你可能已經發現了,這是不可能做出的.Routing(r => r.TypeBased()....)部分額外調用。因此,我可以看到兩個相當簡單的方法:

1:只需將其他參數從外部傳遞到共享配置方法,例如,是這樣的:

var additionalEndpointMappings = new Dictionary<Assembly, string> 
{ 
    { typeof(Whatever).Assembly, "another-queue" } 
}; 
var bus = CreateBus("my-queue", additionalEndpointMappings); 

這當然然後需要在.Routing(...)配置回調適當處理。

2:將所有常用配置拉出成新的擴展方法。我幾乎總是使用這種方法,因爲我發現它很容易維護。

首先,你在一個地方共享庫創建一個新的RebusConfigurer擴展方法:

// shared lib 

public static class CustomRebusConfigEx 
{ 
    public static RebusConfigurer AsServer(this RebusConfigurer configurer, string inputQueueName) 
    { 
     return configurer 
      .Logging(...) 
      .Transport(...)) 
      .Sagas(...) 
      .Serialization(...) 
      .Options(...); 
    }  
} 

,然後你可以通過在終端去

Configure.With(...) 
    .AsServer("my-queue") 
    .Start(); 

調用它。

3的組合(1)和(2),使這個:

Configure.With(...) 
    .AsServer("my-queue") 
    .StandardRouting(r => r.MapAssemblyOf<MessageType>("somewhere-else")) 
    .Start(); 

能最終避免重複的代碼,仍然保持了極大的靈活性的交易,實際上是在尋找漂亮整潔:)

+0

非常感謝,我會嘗試這兩種方法,並得到回覆 –

+0

你可以請提供一個例子1),我不能使用程序集在'MapAssemblyOf ()'因爲它需要**泛型**類型 –