MassTransit: Set topic name after bus has started when when using IBus.ConnectReceiveEndpoint

Viewed 74

I'd like to create temporary subscriptions and queues to an existing topic after the MassTransit bus has started. I can get halfway there by using the following code:


  services.AddMassTransit(a =>
            {
                a.UsingAzureServiceBus((context, b) =>
                {
// !!! This is the code I want to remove and define later on
                    b.Message<MyMessageType>(x =>
                    {
                        // Override to use an existing topic
                        x.SetEntityName("my-existing-topic-name");
                    });
... later on


        var bus = _serviceProvider.GetRequiredService<IBus>();
        var handle = bus.ConnectReceiveEndpoint(new TemporaryEndpointDefinition(), new DefaultEndpointNameFormatter(true), receiveEndpointConfigurator =>
        {

            receiveEndpointConfigurator.Handler<T>(async context =>
            {
                // This is the message handler I'd like to add after the bus is started.
            }

...

However, the problem with the above code is that I need to call the x.SetEntityName before the bus is started. I actually need to configure it inside the ConnectReceiveEndpoint (or immediately before/after).

I've scoured the documentation, I've tried finding APIs that expose the Topology so that I can modify it. I'm at a loss. I found one stackoverflow question that suggested using IBus.ConnectConsumer, instead, but that seems to lead to a dead end. I found an IBus.ConnectHandler, which looks promising, but I'm just guessing at that point.

1 Answers

You can't configure topology once the bus has been started. Any changes to the topic configuration would need to happen before the bus is started.

The only other option is to provide your own IEntityNameFormatter which would be used any time a message type is formatted to an entity name.

Related