Eric
Eric

Reputation: 2067

Rebus with publish/subscribe in two services - how to properly configure rebus

I am having trouble figuring out the proper way to configure rebus when I have two services that are each publishing and subscribing to events from each other. Let's call the services service1 and service2.

Service1 publishes event: Service1.Event1, Service1 subscribes to event: Service2.Event2

Service2 publishes event: Service2.Event2 Service2 consumes event: Service1.Event1

To help understand my confusion, I am not sure if I need to instantiate multiple rebus instances (one for publish and one for subscribe) or if I need only one instance to do both publish and subscribe. I am also uncertain if I need separate queues for each service or if the queue should be shared.

I am using both Azure Service Bus and RabbitMQ in case it matters, but I think that is not important to my question.

Here's what I am trying now (using Azure Service Bus):

Service1 rebus configuration

        // Register handlers
        services.AutoRegisterHandlersFromAssemblyOf<Event2Handler>();

        services.AddRebus(
            (configure, provider) =>
            {
                var rebusConfigurer = configure.Transport(t => t.UseAzureServiceBus(
                        "my connection string",
                        "service1-subscriber-queue"))
                        .Routing(r => r.TypeBased().Map<Event1>("publisher-queue"))
            },

            onCreated: async bus =>
            {
                await bus.Subscribe<Event2>();
            }

        );

Service2 rebus configuration

        // Register handlers
        services.AutoRegisterHandlersFromAssemblyOf<Event1Handler>();

        services.AddRebus(
            (configure, provider) =>
            {
                var rebusConfigurer = configure.Transport(t => t.UseAzureServiceBus(
                        "my connection string",
                        "service2-subscriber-queue"))
                        .Routing(r => r.TypeBased().Map<Event2>("publisher-queue"))
            },

            onCreated: async bus =>
            {
                await bus.Subscribe<Event1>();
            }

        );

Publish event

        await _serviceBus.Publish(the_event);

With this, things are mostly working, but I suspect I am not getting this quite right. One issue I am seeing is that the publish for event1 (from service1) seems to be incorrectly consumed by service1 (by the same service publishing it) and results in an error like the following:

 ---> Rebus.Exceptions.MessageCouldNotBeDispatchedToAnyHandlersException: Message with ID eed9979d-e759-4e95-84a9-c45ca780be4e and type Event1, Events could not be dispatched to any handlers (and will not be retried under the default fail-fast settings)

Any help in understanding how to correctly configure will be much appreciated.

Thanks, Eric

Upvotes: 2

Views: 375

Answers (0)

Related Questions