.net - 服务总线处置对象

标签 .net azure message-queue servicebus

我通过服务总线在 Azure 上使用消息队列架构。有时,当我尝试向队列发送消息时,它会失败。这是我得到的错误:

有时我会收到此消息

Message:Can't create session when the connection is closing. 

其他时候我收到此消息

Message:Cannot access a disposed object.
Object name: 'FaultTolerantAmqpObject`1'. 

请记住,这种情况不会一直发生。有时我会为服务总线创建数千条消息。我正在为发送到队列的每条消息分派(dispatch)一个异步任务

这是我的代码

Task.Run(() => new ServiceBusService().SendQueueMessage(busMessageObject));

服务总线类

public class ServiceBusService
{ 
    static string ServiceBusConnectionString = AzureUtils.SERVICE_BUS_CONNECTIONSTRING;
    const string QueueName = "eventqueue";
    static IQueueClient queueClient;

    public async Task SendQueueMessage(JObject jObject, DateTime? scheduledEnqueueTimeUtc = null)
    {
        string jsonObject = "";
        string scheduledTime = "";

        if(scheduledEnqueueTimeUtc.HasValue)
        {
            scheduledTime = scheduledEnqueueTimeUtc.Value.ToLongTimeString();
        }

        try
        {
            queueClient = new QueueClient(ServiceBusConnectionString, QueueName);
            var message = new Message(Encoding.UTF8.GetBytes(jObject.ToString()));

            if(scheduledEnqueueTimeUtc.HasValue)
                message.ScheduledEnqueueTimeUtc = scheduledEnqueueTimeUtc.Value;

            await queueClient.SendAsync(message);
            await queueClient.CloseAsync();
        }
        catch (Exception e)
        {
            Trace.TraceError($"{Tag()} " + e.InnerException + " " + e.Message);
        }
    }
}

最佳答案

这是因为我的 QueueClient 是静态的,并且多个线程正在使用它并处置它。使其不静态解决了我的问题。

关于.net - 服务总线处置对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55764374/

相关文章:

java - 上传带有java和azure blob存储sdk中子目录的目录

java - 如何使用 weblogic 中定义的 Tibco JMS 模块并使用 Spring 向其发布消息

php - 从队列中读取来自azure服务总线的所有消息

.net - 找不到类 SoapObject

c# - 通过 Controller 使用 ExecuteAsync 测试 IHttpActionResult

.net - 用于 Blob 写入访问的 SAS 不断导致 Azure 存储出现 403 身份验证错误

c# - azure documentdb 创建文档重复项 Id 属性

java - activemq中未加载页面文件异常

c# - 创建一个方法以返回另一个方法的执行时间

c# - C# 中的事件处理 - 无回调