首页 > 解决方案 > 使用 Masstransit 和 Azure 服务总线将故障消息发送到主题订阅死信队列

问题描述

当主题的订阅者抛出异常时,未处理的消息将进入{subscribername}_error队列。

举个例子:

const string subsriberName = "AnotherSubscriber";
cfg.SubscriptionEndpoint<AnotherThingHappened>(host, subsriberName, configurator =>
{
    configurator.Handler<AnotherThingHappened>(context =>
    {
        Console.WriteLine(context.Message.AnotherThingType);
        if (Random.NextDouble() < 0.1)
        {
            throw new Exception("Oups, I failed :(");
        }
        return Task.CompletedTask;
    });
});

它在 ObjectCreatedA 主题上创建了“AnotherSubscriber”订阅。但是当它失败时,消息会进入队列anothersubscriber_error。它使诊断、监控和重播消息变得更加困难。因为从 ASB 的角度来看,这只是一个普通的队列。

在此处输入图像描述

如何将故障路由到主题 ObjectCratedA/AnotherSubscriber 而不是**_error一个的 DLQ?

提前致谢。

标签: masstransit

解决方案


从 MassTransit 6.2 开始,这现在是可能的,请参阅相关的GitHub 问题

您的配置现在需要如下所示:

cfg.SubscriptionEndpoint(
    "my-subscription",
    "my-topic",
    e =>
    {
        e.ConfigureConsumer<MyConsumer>(provider);
        // Send failures to built-in Azure Service Bus Dead Letter queue
        e.ConfigureDeadLetterQueueDeadLetterTransport();
        e.ConfigureDeadLetterQueueErrorTransport();
    });

推荐阅读