Reusing Azure ServiceBus Queue Client Across the Application
Reusing Azure ServiceBus Queue Client Across the Application
In this post, I will show you how to reuse the Azure ServiceBus Queue Client across the application. We will create a Singleton class to manage the Azure ServiceBus Queue Client instance.
Create a console application
Create a new console application using the following command:
1
2
dotnet new console -n ServiceBusQueueClient
cd ServiceBusQueueClient
Install Azure ServiceBus package
Install the Azure.Messaging.ServiceBus
package using the following command:
1
dotnet add package Azure.Messaging.ServiceBus
Create a Service Bus Queue Factory class
Create a new class ServiceBusQueueFactory.cs
and add the following code. This class will be return the ServiceBusSender and ServiceBus Processor instances.
We will register this class as singleton in the DI container.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using Azure.Messaging.ServiceBus;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace ServiceBusQueueClient
{
public class ServiceBusQueueFactory
{
private readonly IConfiguration _configuration;
private readonly IServiceBusClient _serviceBusClient;
private readonly Dictionary<string, ServiceBusSender> _senders = new();
private readonly Dictionary<string, ServiceBusProcessor> _processors = new();
public ServiceBusQueueFactory(IConfiguration configuration)
{
_configuration = configuration;
_serviceBusClient = new ServiceBusClient(_configuration["ServiceBusConnectionString"]);
}
public ServiceBusSender CreateSender(string queueName)
{
if (_senders.ContainsKey(queueName))
{
return _senders[queueName];
}
var sender = _serviceBusClient.CreateSender(queueName);
_senders.Add(queueName, sender);
return sender;
}
public ServiceBusProcessor CreateProcessor(string queueName)
{
if (_processors.ContainsKey(queueName))
{
return _processors[queueName];
}
var processor = _serviceBusClient.CreateProcessor(queueName, new ServiceBusProcessorOptions());
_processors.Add(queueName, processor);
return processor;
}
}
}
Register the ServiceBusQueueFactory class in DI container
Register the ServiceBusQueueFactory
class as singleton in the DI container in the Program.cs
file.
1
2
3
4
5
6
7
8
9
10
using Microsoft.Extensions.DependencyInjection;
var builder = Host.CreateDefaultBuilder(args)
.ConfigureServices((_, services) =>
{
services.AddSingleton<ServiceBusQueueFactory>();
});
await builder.RunConsoleAsync();
Use the ServiceBusQueueFactory class in the Program.cs file
Use the ServiceBusQueueFactory
class in the Program.cs
file to create the ServiceBusSender and ServiceBusProcessor instances.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
var builder = Host.CreateDefaultBuilder(args)
.ConfigureServices((_, services) =>
{
services.AddSingleton<ServiceBusQueueFactory>();
});
var host = builder.Build();
var serviceBusQueueFactory = host.Services.GetRequiredService<ServiceBusQueueFactory>();
var sender = serviceBusQueueFactory.CreateSender("my-queue");
var processor = serviceBusQueueFactory.CreateProcessor("my-queue");
await host.RunConsoleAsync();
Conclusion
In this post, we learned how to reuse the Azure ServiceBus Queue Client across the application. We created a Singleton class to manage the Azure ServiceBus Queue Client instance. We registered the Singleton class in the DI container and used it to create the ServiceBusSender and ServiceBusProcessor instances.
This approach will help you to manage the Azure ServiceBus Queue Client instance efficiently and avoid creating multiple instances of the client across the application.