NameIsError
NameIsError

Reputation: 11

Azure Functions publish to RabbitMQ using MassTransit

I am currently running an Azure Function that is using Masstransit to publish to an Azure Service Bus. For ease of debugging, I would like to be able to run the function locally and publish to a local RabbitMQ instance instead.

On the consumer side everything connects well and I can publish messages through the RabbitMQ Management UI. However on the Function, I keep seeing the following exception when trying to configure RabbitMQ:

MassTransit.ConfigurationException: 'SetBusFactory' can be called only once.

This is my Function Startup.cs

public override void Configure(IFunctionsHostBuilder builder)
    {
        builder
            .Services
            .AddScoped<MyService1>()
            .AddScoped<MyService2>()
            .AddMassTransitForAzureFunctions(massTransitConfig =>
            {
                massTransitConfig.UsingRabbitMq((context, config) =>
                {
                    config.Host("localhost", "/", h =>
                    {
                        h.Username("guest");
                        h.Password("guest");
                    });
                });
                massTransitConfig.SetKebabCaseEndpointNameFormatter();
            }, "AzureWebJobsServiceBus");
    }

I was checking the source code for the AddMassTransitForAzureFunctions method and it seems to automatically add a connection to an Azure Service Bus instance. Does this mean that there is currently no support for RabbitMQ for Azure Functions? Any help or clarification would be appreciated.

Framework - .NET 6.0 Relevant Libraries - MassTransit 8.0.15, MassTransitRabbitMQ 8.0.15

Upvotes: 0

Views: 302

Answers (1)

Sampath
Sampath

Reputation: 3523

  • The error "MassTransit.ConfigurationException: 'SetBusFactory' can be called only once" occurs because AddMassTransitForAzureFunctions configures MassTransit with Azure Service Bus by default. Your attempt to use UsingRabbitMq conflicts with this existing configuration. Thanks to @Md Farid Uddin Kiron for the comment about the method.
public static void Main()
{
    var host = Host.CreateDefaultBuilder(args)
        .ConfigureFunctionsWorkerDefaults()
        .ConfigureServices(services =>
        {
            services.AddMassTransit(x =>
            {
                x.UsingRabbitMq((context, cfg) =>
                {
                    cfg.Host("localhost", "/", h =>
                    {
                        h.Username("guest");
                        h.Password("guest");
                    });
                });
            });
            // ... other services
        })
        .Build();

    host.Run();
}

enter image description here

Another method is to integrate Service Bus with RabbitMQ and use RabbitMQ and MassTransit using the links below:

Upvotes: 0

Related Questions