公共交通和 Azure 服务总线计划重试

Mass Transit and Azure Service Bus Scheduled Retries

我们的公共交通/Azure 服务总线设置运行良好,但还有一项我们非常想使用但似乎无法使用的功能 - 计划重新交付。

这是启动中的代码:

busFactoryConfig.UseServiceBusMessageScheduler();
    
busFactoryConfig.SubscriptionEndpoint(Constants.SubscriptionName, Constants.TopicName, configurator =>
                    {
                        configurator.UseScheduledRedelivery(r => r.Intervals(TimeSpan.FromMinutes(5), TimeSpan.FromMinutes(15), TimeSpan.FromMinutes(30)));
                        configurator.UseMessageRetry(r =>
                        {
                            r.Immediate(2);
                            r.Ignore<InvalidCastException>(); //we don't want to retry here, the message content is invalid
                        });

这似乎与 documentation 匹配得很好。虽然文档好像只提到了RabbitMQ。

但是,当这实际触发时,抛出公共交通异常,并且重新交付不起作用。这是使用 .NET Core 3.1、MassTransit 7.0.1 和 Azure 服务总线主题。

错误(故意抛出以触发重试):

MassTransit.TransportException: **ADDRESS** => The message delivery could not be rescheduled
 ---> System.AggregateException: One or more errors occurred. (The specified resource description is invalid. TrackingId:cda5544e-90e4-4818-97f9-4de3ee55aaad_G46, SystemTracker:**TOPIC**, Timestamp:2020-07-22T13:22:23) (BAM! <- MY ERROR)
 ---> Microsoft.Azure.ServiceBus.ServiceBusException: The specified resource description is invalid. TrackingId:cda5544e-90e4-4818-97f9-4de3ee55aaad_G46, SystemTracker:**TOPIC**, Timestamp:2020-07-22T13:22:23
 ---> System.ArgumentException: The specified resource description is invalid. TrackingId:cda5544e-90e4-4818-97f9-4de3ee55aaad_G46, SystemTracker:**TOPIC**, Timestamp:2020-07-22T13:22:23

我尝试了不同的设置,更类似于文档:

busFactoryConfig.UseMessageScheduler(uri); //Uri is my sb endpoint, minus Endpoint=

这给我们带来了不同的错误:

InvalidAudience: The authorization header contains a token with a wrong audience.

快速阅读会发现这与 Azure 使用密钥的方式有关,并且它可能已过时。但是,这与我用来成功连接到总线的 URI 相同——我所做的只是删除 Endpoint=,以便在构造 new Uri() 时它是一个有效的 URI。

计划的重新交付是否适用于 Azure 服务总线的 MT?我在配置中遗漏了什么吗?

如有任何帮助,我们将不胜感激。

您不能使用订阅端点进行重新传送。如果您想使用重新传递,我建议使用常规的 ReceiveEndpoint(消息将从主题转发到队列,队列由 MassTransit 使用消费者消息类型配置)。

订阅端点特定于 ASB,通常仅在您有要直接订阅的现有主题时使用。

正如 Chris 在上面回答的那样 - SubscriptionEndpoints 不受此支持,您必须改用 ReceiveEndpoint。这对我有用:

busFactoryConfig.UseServiceBusMessageScheduler();

busFactoryConfig.ReceiveEndpoint(Constants.SubscriptionName, configurator =>
                    {
                        configurator.UseScheduledRedelivery(r => r.Intervals(TimeSpan.FromMinutes(1), TimeSpan.FromMinutes(2), TimeSpan.FromMinutes(3)));
                        configurator.UseMessageRetry(r =>
                        {
                            r.Immediate(2);
                            r.Ignore<InvalidCastException>(); //we don't want to retry here, the message content is invalid
                        });

                        configurator.ConfigureConsumer<MyConsumer>(context);
                    }