You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET中Azure Service Bus消息偶发因超最大发送次数进入DLQ问题

问题:Azure Service Bus + Mass Transit 消息偶发进入DLQ(提示“maximum number of sends was exceeded”)

场景概述

在.NET WebApi应用中使用Azure Service Bus和Mass Transit库,具体流程:

  • ServiceA发布StartProccess消息,该消息同时用于启动状态机StartStateMachine
  • 消息发送至ServiceB(Azure Functions)进行处理并转发
  • 由于Azure Functions要求订阅必须预先存在,无法通过Mass Transit自动创建绑定(Mass Transit需要本地有消费者实现),因此在ServiceA中通过Azure Service Bus管理API手动创建主题和订阅,代码如下:
if (!(await adminClient.TopicExistsAsync(formatter.MessageName<StartProccess>()));
    adminClient.CreateTopicAsync(formatter.MessageName<StartProccess>());
    
if (!(await adminClient.SubscriptionExistsAsync(formatter.MessageName<StartProccess>()), formatterCheckFunc.ConsumerFromMessageName<StartProccess>()).Value)
    await adminClient.CreateSubscriptionAsync(new CreateSubscriptionOptions(formatter.MessageName<StartProccess>(), formatterCheckFunc.ConsumerFromMessageName<StartProccess>()) { MaxDeliveryCount = 1 });

设置MaxDeliveryCount=1是为了避免消费者异常时重复执行10次,期望实现类似Mass Transit的效果:出错时立即触发IFault逻辑,不重复调用。

问题现象

整体运行正常,但偶尔(约1%的概率)会有StartProccess消息因提示“maximum number of sends was exceeded”进入死信队列(DLQ),导致状态机无法启动,该问题难以复现。

补充:ServiceB消息处理代码

ServiceB中的消息处理逻辑如下:

try
{
    await _receiver.HandleConsumer<Consumer>(TopicName, SubscriptionName, message, cancellationToken);
    await messageActions.CompleteMessageAsync(message);
}
catch (SomeException ex)
{
    await messageActions.AbandonMessageAsync(message);
    _log.LogWarning("LogIt");
}
catch
{
    // 此处存在疑问:应该用哪种处理方式?
    await messageActions.CompleteMessageAsync(message);
    // 还是这个?
    await messageActions.DeadLetterMessageAsync(message);
    throw;
}

内容的提问来源于stack exchange,提问作者Petr Klekner

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 22:52:14