gpt4 book ai didi

c# - 服务总线 - 单例连接类?

转载 作者:太空狗 更新时间:2023-10-29 23:30:02 27 4
gpt4 key购买 nike

我正在尝试找出从 Web-API 使用服务总线的最佳实践是什么。

我读到,重新创建 QueueClient、SubscriptionClient 等对象是错误的方法,因此我需要重用工厂和客户端。

Service Bus client objects, such as Microsoft.ServiceBus.Messaging.QueueClient or Microsoft.ServiceBus.Messaging.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 can be avoided by re-using the same factory and client objects for multiple operations.

reference

我需要实现一个特殊的类来保存与服务总线的连接,我正在考虑一个单例类来保存特定的操作(像EnqueueJobToArchiveQueue(Job job)这样的函数和构造函数会初始化“具体操作函数”会用到的QueueClient、MessageFactory等。

我的问题是我需要关闭对象(QueueClient.Close()),我什么时候需要关闭对象?

到目前为止,这是我的类(class):

 public class ServiceBusHelper
{
private static readonly ServiceBusHelper instance = new ServiceBusHelper();

private static MessagingFactory msgFactory;

private static NamespaceManager namespaceManager;

private const string jobQueueName = "job";

private const string responseTopicName = "jobResult";

private const string archiveQueueName = "jobArchive";

private static QueueClient archiveQueue;

private static QueueClient jobQueue;

private static TopicClient responseTopic;

private ServiceBusHelper()
{

}

static ServiceBusHelper()
{

msgFactory = MessagingFactory.CreateFromConnectionString(ConfigurationManager.AppSettings["ServiceBusCS"]);
namespaceManager = NamespaceManager.CreateFromConnectionString(ConfigurationManager.AppSettings["ServiceBusCS"]);

if (!namespaceManager.QueueExists(jobQueueName))
{
namespaceManager.CreateQueue(jobQueueName);
}

filteringQueue = QueueClient.CreateFromConnectionString(ConfigurationManager.AppSettings["ServiceBusCS"], jobQueueName);

if (!namespaceManager.QueueExists(archiveQueueName))
{
namespaceManager.CreateQueue(archiveQueueName);
}

archiveQueue = QueueClient.CreateFromConnectionString(ConfigurationManager.AppSettings["ServiceBusCS"], archiveQueueName);

if (!namespaceManager.TopicExists(responseTopicName))
{
namespaceManager.TopicExists(responseTopicName);
}

responseTopic = TopicClient.CreateFromConnectionString(ConfigurationManager.AppSettings["ServiceBusCS"],responseTopicName);


}

public static ServiceBusHelper Instance
{
get
{
return instance;
}
}


public void EnququeJobToDo(Job job, string corrId)
{

// Compose the message
BrokeredMessage msg = new BrokeredMessage(job);

msg.CorrelationId = corrId;

// Send the message
filteringQueue.Send(msg);
}
}

如您所见,我没有关闭连接(QueueClient.Close()),我应该在哪里关闭连接?使用 Dispose() 实现 IDisposable ?

如果有更好的方法,如果您能与我分享,我将不胜感激。

此代码来自具有不错工作负载的 Web-API(Azure 云服务)。

更新

我已经使用 Dispose() 更新了我的类,如下所示:

     public void Dispose()
{
if (msgFactory != null)
{
msgFactory.Close();
}

}

最佳答案

服务总线 SDK 使用的默认底层协议(protocol)是专有的 SBMP(服务总线消息传递协议(protocol)),该协议(protocol)在 TCP/IP 连接之上工作,关闭工厂时该连接也会关闭。如果您选择使用 TransportType=Amqp(在连接字符串中),您可以切换到 AMQP 协议(protocol)。在这种情况下,工厂处理与总线的唯一 TCP 连接,并且 QueueClient、TopicClient 类(从工厂创建)实例化上述 TCP 连接内的 session 和链接。 session 和链接是用于在单个 TCP 连接上进行多路复用的两个 AMQP 概念。如果仅关闭QueueClient和TopicClient,则关闭操作仅关闭相关 session 和链接,而不会关闭关闭工厂对象时关闭的TCP连接。当然我不知道 SBMP 内部是如何工作的,因为它是一个专有协议(protocol)。但是,在处置中,您可以关闭工厂和相关的队列/主题对象。你有什么问题吗?

关于c# - 服务总线 - 单例连接类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33512803/

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