gpt4 book ai didi

azure - 在向 Azure 主题发送消息之前重新创建主题客户端是否是一个好习惯

转载 作者:行者123 更新时间:2023-12-02 03:43:24 24 4
gpt4 key购买 nike

我正在使用 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帖子:

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 对象。

关于azure - 在向 Azure 主题发送消息之前重新创建主题客户端是否是一个好习惯,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47715117/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com