I am utilizing Azure Service Bus with a .NET Core 3.1 application. My application receives thousands of requests and places them in the topic. The question I want to ask is whether I should create a new sender for each message or if a single sender should process all messages.
My current implementation is:
private async Task EnqueueAsync(ServiceBusMessage message, string queueOrTopicName)
{
var sender = _serviceBusClient.CreateSender(queueOrTopicName);
try
{
await sender.SendMessageAsync(message);
_logger.Info($"Message has been published to {queueOrTopicName}");
}
catch (ServiceBusException ex)
{
_logger.Error($"Error while publishing data to the Service bus: {ex}");
throw new QueueException("Unable to publish data.");
}
finally
{
await sender.DisposeAsync();
}
}
ServiceBusClient is registered as a Singleton and the topic has 5 subscriptions so far.
2
Answers
Based on the following 2 docs, I can safely use a single sender to process all messages.
I've created a IServicebusSenderProvider and registered it as a singleton and in the constructor of the class, I've called
this._serviceBusClient.CreateSender(topicName);
and then used IServiceBusSenderProvider in MessageSender class.https://github.com/Azure/azure-service-bus/issues/245 https://learn.microsoft.com/en-us/azure/service-bus-messaging/service-bus-performance-improvements?tabs=net-standard-sdk-2
Below is
code
with which i am able to send multiple messages by creating one sender instance:Output:
You can integrate your code with your messages.