在向 Azure 主题发送消息之前重新创建主题客户端是一种好习惯吗

Is it a good practice to re-create a Topic Client before sending a message to Azure Topic

我正在使用 Microsoft.Azure.ServiceBus,版本=2.0.0.0 程序集连接到 Azure 主题。代码如下

public void SendMessage(Message brokeredMessage) 
{
    var topicClient = new TopicClient(_configuration.ConnectionString, topicName, _defaultRetryPolicy);
    await topicClient.SendAsync(brokeredMessage);
    await topicClient.CloseAsync();
}

我想知道每次我需要向主题发送消息时都创建主题客户端是一个好习惯,还是我应该在应用程序启动时创建主题客户端并在每次需要时继续使用同一个客户端要发送消息吗?

是否有任何我需要考虑的性能或可扩展性问题?

来自 Azure Service Bus Best Practices post:

Reusing factories and clients

Service Bus client objects, such as QueueClient or MessageSender, are created through a MessagingFactory object, which also provides internal management of connections. You should not close messaging factories or queue, topic, and subscription clients after you send a message, and then re-create them when you send the next message. Closing a messaging factory deletes the connection to the Service Bus service, and a new connection is established when recreating the factory. Establishing a connection is an expensive operation that you can avoid by re-using the same factory and client objects for multiple operations. You can safely use the QueueClient object for sending messages from concurrent asynchronous operations and multiple threads.

基于此,您应该重用 Topic Client 对象。