为什么在使用 Azure 服务总线更新到 .NET 5 后 Rebus.Send 不工作

Why is Rebus.Send not working after update to .NET 5 using Azure Service Bus

我们有一个 .net core 3.1 web 项目运行 Rebus 和 Azure 服务总线。

更新到 .NET 5 后,我们无法再使用 Rebus 将消息发送到 Azure 服务总线队列。没有进行任何代码更改,仅更新到 .NET 5 和最新的 Rebus-packages(6.6.1)。

var contractMessage = _manager.GetContractMessage();
await _bus.Send(contractMessage);

这抛出:

Exception
    Rebus.Exceptions.RebusApplicationException: Could not send to queue 'sbq-queue-queue1'
 ---> System.InvalidOperationException: Operation is not valid due to the current state of the object.
   at Microsoft.Azure.ServiceBus.Core.MessageSender.OnSendAsync(IList`1 messageList)
   at Microsoft.Azure.ServiceBus.RetryPolicy.RunOperation(Func`1 operation, TimeSpan operationTimeout)
   at Microsoft.Azure.ServiceBus.RetryPolicy.RunOperation(Func`1 operation, TimeSpan operationTimeout)
   at Microsoft.Azure.ServiceBus.Core.MessageSender.SendAsync(IList`1 messageList)
   at Rebus.AzureServiceBus.AzureServiceBusTransport.<>c__DisplayClass36_0.<<GetOutgoingMessages>g__SendOutgoingMessagesToDestination|3>d.MoveNext()
   --- End of inner exception stack trace ---
   at Rebus.AzureServiceBus.AzureServiceBusTransport.<>c__DisplayClass36_0.<<GetOutgoingMessages>g__SendOutgoingMessagesToDestination|3>d.MoveNext()
--- End of stack trace from previous location ---
   at Rebus.AzureServiceBus.AzureServiceBusTransport.<>c__DisplayClass36_1.<<GetOutgoingMessages>g__SendOutgoingMessages|1>d.MoveNext()
--- End of stack trace from previous location ---
   at Rebus.Transport.TransactionContext.InvokeAsync(Func`2 actions)
   at Rebus.Transport.TransactionContext.Complete()
   at Rebus.Bus.RebusBus.InnerSend(IEnumerable`1 destinationAddresses, Message logicalMessage)
   at Rebus.Bus.RebusBus.Send(Object commandMessage, IDictionary`2 optionalHeaders)
   at xx.xx.xx.xx.xx.someclass.request.mediatrHandler.Handle(ClassRequest request, CancellationToken cancellationToken) in /home/vsts/work/1/s/xx.xx.xx/Application/xx/xx/xxRequest.cs:line 187

奇怪的是,这似乎在本地调试时有效,但在生产服务器上无效,这些服务器是 azure 虚拟机 运行 IIS。与在 IIS 中本地测试时相同的托管包。

为了连接到 ASB,我尝试了托管标识和完整连接字符串。

使用包:

Rebus 6.6.1
Rebus.AzureServiceBus 9.0.5
Rebus.Serilog 6.0.0
Rebus.Serviceprovider 6.4.1

并在 ConfigureServices 中设置 Rebus:

  services.AddRebus((options, serviceProvider) => options
    .Logging(l => l.Serilog())
    .Transport(t => t.UseAzureServiceBusAsOneWayClient(configuration["RebusConfig:AzureServiceBusConnectionString"]))
    .Routing(r => {
      r.TypeBased().MapAssemblyOf<Contract1>(configuration["RebusConfig:Routes:Route1"])
        .MapAssemblyOf<Contract2>(configuration["RebusConfig:Routes:Route2"]);
    })
  );

将 Microsoft Azure 包更新到最新版本并将连接切换到所有 Azure 服务的新 DefaultAzureCredentials() 后,此问题自行解决。