服务总线处理对象

问题描述 投票:0回答:1

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

有时我收到这条消息

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

其他时候我收到此消息

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

请记住,它并非始终发生。有时我会为服务总线创建数千条消息。我正在为发送到队列的每条消息调度异步任务

这是我的代码

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

ServiceBus类

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);
            }
        }
}
.net azure message-queue servicebus
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.