Search code examples
.netazure-functions.net-6.0azureservicebusmasstransit

What is the alternative to CreateBrokeredMessageReceiver in MassTransit 7 and above?


I am upgrading a .NET 3.1 application straight to .NET 6. It uses Azure Service Bus and a function app to handle commands and pass them to the correct consumers (CQRS pattern).

I use Bus.Factory.CreateBrokeredMessageReceiver (in .NET 3.1) to form the handler but since upgrading to .NET 6 (and updating all MassTransit packages to v7), this has been removed. What is the alternative approach? I have numerous consumers and the single consumer solutions don't help.

[FunctionName("MessageHandler")]
public async Task Run(
    [ServiceBusTrigger("%ServiceBusQueue%", Connection = "ServiceBusConnection")]
        ServiceBusReceivedMessage message,
        CancellationToken cancellationToken)
{
    ...
    var handler = Bus.Factory.CreateBrokeredMessageReceiver(
        binder,
        cfg =>
        {
            cfg.CancellationToken = cancellationToken;
            cfg.SetLog(logger);
            cfg.InputAddress = new Uri(_settings.ServiceBusUrl);
            cfg.UseRetry(x => x.Intervals(2000, 4000, 8000, 16000));
            cfg.Consumer(GetConsumerType(messageType), type => _serviceProvider.GetService(type));
        });

    await handler.Handle(message);
    ...
}

private Type GetConsumerType(string messageType)
{
    ...
    return consumerType; 
}

I have upgraded all relevant packages and use the following for the function app:

  • MassTransit.Azure.ServiceBus.Core
  • MassTransit.Extensions.DependencyInjection
  • MassTransit.WebJobs.ServiceBus

I have also followed some alternatives online (e.g. those from Chris Patterson re. IMessageReceiver) but to no avail.


Solution

  • I would suggest reviewing the Azure Function sample to understand how to use the latest version of MassTransit on Azure Service Bus.