gpt4 book ai didi

c# - Azure ServiceBus MessageSender 类在长期进程中作为单例运行是否安全?

转载 作者:行者123 更新时间:2023-11-30 15:52:01 24 4
gpt4 key购买 nike

我有一个长时间运行的进程,它会唤醒、执行某些任务,并且可能需要也可能不需要通过 MessageSender 将消息发布到 Azure 服务总线。如果我可以使 MessageSender 成为单例,这会稍微简化我的代码,所以我希望它是否可行。

为了澄清,我预计这个过程可能会持续很长一段时间(至少几个小时,可能几天)而不向服务总线发送任何消息。

super 简化的示例:

public async Task WorkLoop(CancellationToken token)
{
while (!token.IsCancellationRequested)
{
var result = DoWork();
if (result.shouldPublish)
{
var message = buildMessage(result);
await _messageSender.SendAsync(message);
}
await Task.Delay(sleepDuration, token);
}
}

MessageSender 实例长时间(至少几天,可能几周或几个月)保持事件状态是否会产生任何后果?

最佳答案

根据Best Practices for performance improvements using Service Bus Messaging :

It is recommended that you do 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. [...] You can safely use these client objects for concurrent asynchronous operations and from multiple threads.

关于c# - Azure ServiceBus MessageSender 类在长期进程中作为单例运行是否安全?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55582125/

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