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

同一宿主中NServiceBus 7双端点配置异常:仅首个端点生效

解决NServiceBus 7同一宿主多端点仅第一个识别处理程序的问题

问题核心是NServiceBus的默认程序集扫描机制在多端点场景下会出现冲突——第一个端点启动时完成了全局扫描,第二个端点复用了已有的扫描结果,导致无法识别专属的处理程序。

解决方法是为每个端点显式指定要扫描的程序集或类型范围,避免依赖默认扫描:

方案1:按程序集拆分处理程序

如果两个端点的处理程序分别在不同的程序集中,直接指定各自的程序集:

// 第一个端点配置
var endpointConfiguration1 = new EndpointConfiguration(Queue1);
// 显式指定扫描的程序集
endpointConfiguration1.AssembliesToScan(Assembly.Load("Endpoint1Handlers"));
// 其他原有配置
endpointConfiguration1.SendFailedMessagesTo("error");
endpointConfiguration1.UseSerialization<JsonSerializer>();
endpointConfiguration1.EnableInstallers();
endpointConfiguration1.UsePersistence<InMemoryPersistence>();

_endpointInstance1 = await Endpoint.Start(endpointConfiguration1).ConfigureAwait(false);

// 第二个端点配置
var endpointConfiguration2 = new EndpointConfiguration(Queue2);
// 显式指定扫描的程序集
endpointConfiguration2.AssembliesToScan(Assembly.Load("Endpoint2Handlers"));
// 其他原有配置
endpointConfiguration2.SendFailedMessagesTo("error");
endpointConfiguration2.UseSerialization<JsonSerializer>();
endpointConfiguration2.EnableInstallers();
endpointConfiguration2.UsePersistence<InMemoryPersistence>();

_endpointInstance2 = await Endpoint.Start(endpointConfiguration2).ConfigureAwait(false);

方案2:按命名空间/类型过滤处理程序

如果处理程序在同一个程序集里,通过命名空间或类型特征区分:

// 第一个端点配置
var endpointConfiguration1 = new EndpointConfiguration(Queue1);
// 只扫描属于Endpoint1命名空间的处理程序
endpointConfiguration1.TypesToScan(type => 
    type.Namespace != null && type.Namespace.StartsWith("YourApp.Endpoint1"));
// 其他原有配置
endpointConfiguration1.SendFailedMessagesTo("error");
endpointConfiguration1.UseSerialization<JsonSerializer>();
endpointConfiguration1.EnableInstallers();
endpointConfiguration1.UsePersistence<InMemoryPersistence>();

_endpointInstance1 = await Endpoint.Start(endpointConfiguration1).ConfigureAwait(false);

// 第二个端点配置
var endpointConfiguration2 = new EndpointConfiguration(Queue2);
// 只扫描属于Endpoint2命名空间的处理程序
endpointConfiguration2.TypesToScan(type => 
    type.Namespace != null && type.Namespace.StartsWith("YourApp.Endpoint2"));
// 其他原有配置
endpointConfiguration2.SendFailedMessagesTo("error");
endpointConfiguration2.UseSerialization<JsonSerializer>();
endpointConfiguration2.EnableInstallers();
endpointConfiguration2.UsePersistence<InMemoryPersistence>();

_endpointInstance2 = await Endpoint.Start(endpointConfiguration2).ConfigureAwait(false);

额外检查点

  • 确认处理程序类正确实现了IHandleMessages<T>接口,没有命名空间或类型拼写错误
  • 修改配置后查看bin目录下的configuration.txt,验证每个端点的扫描范围是否包含对应处理程序

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 15:14:52