1

We have been using Rebus to send commands to Azure Service Bus. We have a project that spans environments and needs to send commands to two different ASB namespaces (different connection strings).

The way we currently register Rebus doesn't allow us to create a factory or use multiple namespaces (that I'm aware of).

Inside Startup.cs ConfigureServices(...) method:

services.AddRebus(config =>
{
    var asbConfig = Configuration.GetSection("AzureServiceBusConfiguration").Get<AzureServiceBusConfiguration>();
    config
        .Logging(l => l.Serilog(Log.Logger))
        .Transport(t => t.UseAzureServiceBusAsOneWayClient(asbConfig.ConnectionString))
        .Routing(r => r.TypeBased().Map<MyCommand>($"{asbConfig.Environment}/myQueueName"));

    return config;
});

I've tried attacking this from several different directions, and all have fallen short. Is there a supported way to register more than one IBus configuration with different connection strings?

We basically need to spin this up per request scope so we can configure Rebus based on a request header value. Not sure where to start with this.

Kizmar
  • 2,465
  • 3
  • 20
  • 27

1 Answers1

1

While Rebus has pretty good support for inserting itself into an IoC container via the "container adapter" concept, it doesn't necessarily make sense to always make it do so automatically.

In this case, I suggest you wrap one-way clients a dedicated class, e.g. something like a CommandSender or something, and then the command sender can initialize its one-way client in the constructor (and dispose it again in its Dispose method).

One-way clients are fairly inexpensive to create, so it might be ok to simply create/dispose every time you need them. If you need them often though, I suggest you use a ConcurrentDictionary to store the initialized instances – just remember to dispose them all when your application shuts down.

mookid8000
  • 18,258
  • 2
  • 39
  • 63