Azure 服务总线上的 MT。为什么我会看到 System.InvalidOperationException:连接关闭时无法创建会话?

MT on Azure Service Bus. Why am I seeing System.InvalidOperationException: Can't create session when the connection is closing?

我正在使用 MT 5.5.6 和 Azure 服务总线

我偶尔会在我的所有服务中收到以下异常。

System.InvalidOperationException: Can't create session when the connection is closing.
at Microsoft.Azure.ServiceBus.Core.MessageReceiver.OnReceiveAsync(Int32 maxMessageCount, TimeSpan serverWaitTime)

大多数时候一切都按预期工作,但偶尔我会收到异常。 我没有使用 ASB 的会话功能。

我有一个用于从存储库接收大型消息数据的简单配置:

    x.AddBus(context => Bus.Factory.CreateUsingAzureServiceBus(sbc =>
    {
        var componentContext = context.Resolve<IComponentContext>();
        var host = sbc.Host(new Uri(_appSettings.GetSection("Azure")["ServiceBusUri"]), h =>
        {
            h.SharedAccessSignature(s =>
            {
                s.KeyName = _appSettings.GetSection("Azure")["ServiceBusKeyName"];
                s.SharedAccessKey = _appSettings.GetSection("Azure")["SharedAccessKey"];
                s.TokenTimeToLive = TimeSpan.FromDays(1);
                s.TokenScope = TokenScope.Namespace;
            });
        });

        sbc.ReceiveEndpoint(host, _appSettings.GetSection("Azure")["ReceiveQueue"], ep =>
        {
            ep.PrefetchCount = 16;
            ep.Consumer<BatchDocumentPresentedConsumer>(componentContext);
            ep.UseMessageData<BatchDocumentReceived>(componentContext.Resolve<IMessageDataRepository>());
        });

        sbc.UseSerilog();
    }));
});

这是来自 ASB 的队列详细信息(使用 Get-AzServiceBusQueue):

Name                                : filter
LockDuration                        : PT5M
AccessedAt                          : 11/05/2020 06:24:18
AutoDeleteOnIdle                    : P427D
CreatedAt                           : 12/03/2020 06:22:23
DefaultMessageTimeToLive            : P366D
DuplicateDetectionHistoryTimeWindow : PT10M
DeadLetteringOnMessageExpiration    : True
EnableExpress                       : False
EnablePartitioning                  : False
MaxDeliveryCount                    : 5
MaxSizeInMegabytes                  : 1024
MessageCount                        : 0
CountDetails                        : Microsoft.Azure.Management.ServiceBus.Models.MessageCountDetails
RequiresDuplicateDetection          : False
RequiresSession                     : False
SizeInBytes                         : 0
Status                              : Active
UpdatedAt                           : 12/03/2020 06:22:23
ForwardTo                           :
ForwardDeadLetteredMessagesTo       :
EnableBatchedOperations             : True

为什么我会看到这个异常?会不会是ASB不可用?

我相信 MassTransit 的那个版本,当应用程序和 Azure 服务总线之间的连接中断时会出现问题,它会回收连接,然后重新启动所有消费者。但是,当连接指示它正在停止时,消费者没有正确退出循环,因此接收方将尝试重新启动,但最终失败了。

所以,关于 ASB 不可用的最后一个问题,很可能是连接中断或 AMQP 套接字因任何原因(发生暂时性故障)而断开连接,并且是在 reconnection/restart 期间抛出的消费者。