问:如何在Rebus中扩展路由配置

时间:2017-08-09 13:04:48

标签: rebus

我有一个 rebus配置项目,这是许多网络API项目的共享

所以基本上,它看起来像

Web api 1 ==> 共享Rebus配置

Web api 2 ==> 共享Rebus配置

Web api 3 ==> 共享Rebus配置

我的问题是,如果我有一些消息& Web 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();

1 个答案:

答案 0 :(得分: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();

最终可以避免重复代码,仍然保留了很大的灵活性,实际上看起来非常整洁:)