我通过服务总线在 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/