2017-08-09 78 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 ()'因为它需要**泛型**类型 –