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.
Below is
codewith which i am able to send multiple messages by creating one sender instance:Output:You can integrate your code with your messages.