简体   繁体   中英

ServiceBusClient for Azure Service Bus - reuse tcp connections like with HttpClientFactory

I have many CRUD APIs that are communication with each other over HTTP calls (.NET 5)
To avoid too many open tcp connections, I use HttpClientFactory via DI in all those services. This works well and I do not experience too many connections that are opened via HTTP.

But my Azure App Services are still complaining about too many SNAT-connections:

在此处输入图像描述

I guess that the reason is the Azure Service Bus. Each call of my APIs will write events to the bus.
To do this I create a new instance on each call:

            await using (ServiceBusClient client = new ServiceBusClient(_serviceBusConnectionString))
                {
                    var messageObject = new { message.Name, message.Body };
                    var messageJson = JsonConvert.SerializeObject(messageObject);
                    ServiceBusSender sender = client.CreateSender(_topicName);
                    await sender.SendMessageAsync(new ServiceBusMessage(messageJson));
                }

Many developers are using HttpClient like this (what is a bad idea, read why in the article above). The solution for HttpClient is the AddHttpClient method, provided by .NET for this purpose.

But what about Azure Service Bus? There is nothing like an AzureServiceBusFactory or so and adding the AzureServiceBus as a Singleton would not be a good idea, because the configuration should be different on each call.

How can I make sure that the connection pool is also reused for Azure Service Bus connections? Is there any best practice I missed? Or do you think the problem with the connections has another cause?

Edit:

The accepted answer is right. This works fine and my errors disappeared. Just use the following to add the service bus client:

            services.AddAzureClients(cfg =>
            {
                cfg.AddServiceBusClient("your-connection-string");
            });

After this, you can easily get the client via DI in all your services.

But what about Azure Service Bus? There is nothing like an AzureServiceBusFactory or so and adding the AzureServiceBus as a Singleton would not be a good idea, because the configuration should be different on each call.

It is actually the other way round. Service Bus team recommends that connection to your Service Bus should be registered as Singleton only using ServiceBusClientBuilderExtensions and should not be closed or disposed after every operation.

From this link :

The Service Bus objects that interact with the service, such as ServiceBusClient, ServiceBusSender, ServiceBusReceiver, and ServiceBusProcessor, should be registered for dependency injection as singletons (or instantiated once and shared). ServiceBusClient can be registered for dependency injection with the ServiceBusClientBuilderExtensions.

We recommend that you don't close or dispose these objects after sending or receiving each message. Closing or disposing the entity-specific objects (ServiceBusSender/Receiver/Processor) results in tearing down the link to the Service Bus service. Disposing the ServiceBusClient results in tearing down the connection to the Service Bus service.

Please see this link for complete recommendations from Service Bus team: https://docs.microsoft.com/en-us/azure/service-bus-messaging/service-bus-performance-improvements?tabs=net-standard-sdk-2 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM