What is alternate of ServiceBus MessagingFactory in .Net Core?

Viewed 2196

While converting my project from .Net framework 4.7 to .Net core 2.1, I'm facing issue with Servicebus MessagingFactory. I don't see any MessagingFactory class in new nuget package Microsoft.Azure.ServiceBus for .Net core.

My .Net framework 4.7 Code

private static readonly string messagingConnectionString = Environment.GetEnvironmentVariable("ServiceBusConnection");

    private static Lazy<MessagingFactory> lazyMessagingFactory = new Lazy<MessagingFactory>(() =>
    {
        return MessagingFactory.CreateFromConnectionString(messagingConnectionString);
    });

    public static MessagingFactory MessagingFactory
    {
        get
        {
            return lazyMessagingFactory.Value;
        }
    }

public static MessagingFactory EventHubMessageFactory
    {
        get
        {
            return lazyEventhubMessagingFactory.Value;
        }
    }

public async Task SendMessageToQueueAsync(string queueName, string message)
    {
        QueueClient queueClient = MessagingFactory.CreateQueueClient(queueName);
        BrokeredMessage brokeredMessage = new BrokeredMessage(new MemoryStream(Encoding.UTF8.GetBytes(message)), true);
        await queueClient.SendAsync(brokeredMessage);
    }

It was a best practices for high performance application, Also I have many queues under single service bus namespace and I push message based on configuration. I don't want to create QueueClient object in every request and don't want to maintain connection string for every queue.

What is alternate of MessagingFactory in .Net Core?

2 Answers

https://github.com/MicrosoftDocs/azure-docs/issues/46830

https://github.com/Azure/azure-service-bus-dotnet/issues/556

While MessagingFactory is gone, the idea of connection pooling and sharing connections is there. When you create your clients, passing a connection will reuse it. When passing a connection string, will cause clients to establish a new connection.

So you can manually create a ServiceBusConnection or reuse one of an existing client. You can pass the connection object in the constructors of the clients you create. Take care not to close a connection accidentally, e.g. by closing the client that created it.

There are major changes when you are migrating .NetFramework code into .Netcore, you can see Guide for migrating to Azure.Messaging.ServiceBus from Microsoft.Azure.ServiceBus

Example below

static void Main(string[] args)
    {
        string connectionString = "<connection_string>";
        string queueName = "<queue_name>";
        // since ServiceBusClient implements IAsyncDisposable we create it with "await using"
        var client = new ServiceBusClient(connectionString);

        // create the sender
        ServiceBusSender sender = client.CreateSender(queueName);
        for (int i = 0; i < 10; i++)
        {
            // create a message that we can send. UTF-8 encoding is used when providing a string.
            ServiceBusMessage message = new ServiceBusMessage($"Hello world {i}!");

            // send the message
            sender.SendMessageAsync(message).GetAwaiter().GetResult();
        
        }
    
    sender.DisposeAsync().GetAwaiter().GetResult();
    client.DisposeAsync().GetAwaiter().GetResult();
   }
Related