gpt4 book ai didi

c# - NServiceBus 警告 "No handlers could be found for message type"

转载 作者:太空狗 更新时间:2023-10-29 22:33:24 24 4
gpt4 key购买 nike

我是 NServiceBus 的新手,正在尝试开发一个发布者和单独的订阅者(我使用的是 v3.2.0.0),到目前为止,它工作正常——发布者和订阅者都在 NServiceBus 主机中运行.我的消息全部发布正常,但间歇性地没有被订阅者接收,发布者显示以下错误:

2012-09-05 14:27:37,491 [Worker.6] WARN  NServiceBus.Unicast.UnicastBus [(null)]  <(null)> - No handlers could be found for message type: MyNamespace.MyMessage

虽然所有消息都出现在 MSMQ 队列中,但此警告并未出现在所有消息中,因此如果我继续发布一条消息,我可能会看到其中一半显示消息,因此没有被订阅者接收到。

我承认我很难理解这个问题,所以到目前为止我的一些代码很可能完全是垃圾!

我按如下方式向 NSB 发布消息,消息输入是我定义的几种不同类型之一:

private void Publish<T>(T message)
{
var myBus = Configure.Instance.Builder.Build<IBus>();
myBus.Publish(message);
}

发布者的EndpointConfig如下:

[EndpointName("MyQueue")]
public class EndpointConfig : IConfigureThisEndpoint, AsA_Publisher, IWantCustomInitialization
{
/// <summary>
/// Initialisation for NServiceBus.
/// </summary>
public void Init()
{
Configure.With()
.DefaultBuilder()
.MsmqSubscriptionStorage()
.DisableTimeoutManager()
.DefiningEventsAs(t => t.Namespace != null && t.Namespace.StartsWith("MyNamespace"));
}
}

在订阅者端,我有以下 EndpointConfig:

[EndpointName("MyQueue")]
public class EndPointConfig : IConfigureThisEndpoint, AsA_Server, IWantCustomInitialization
{
public void Init()
{
Configure.With()
.DefiningEventsAs(t => t.Namespace != null && t.Namespace.StartsWith("MyNamespace"));
}
}

使用如下的 EventMessageHandler:

public class EventMessageHandler : IEvent, IHandleMessages<IMyMessage>
{
public void Handle(IMyMessage message)
{
Console.WriteLine(string.Format("Subscriber 1 received EventMessage with Id {0}.", message.Id));
}
}

订阅者的 app.config 是:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="UnicastBusConfig" type="NServiceBus.Config.UnicastBusConfig, NServiceBus.Core" />
<section name="MessageForwardingInCaseOfFaultConfig" type="NServiceBus.Config.MessageForwardingInCaseOfFaultConfig, NServiceBus.Core" />
</configSections>

<MessageForwardingInCaseOfFaultConfig ErrorQueue="error"/>

<UnicastBusConfig>
<MessageEndpointMappings>
<add Messages="MyNamespace" Endpoint="MyQueue" />
</MessageEndpointMappings>
</UnicastBusConfig>
</configuration>

最佳答案

看起来您为发布者和订阅者使用了相同的端点名称。 NServiceBus 使用端点名称生成队列名称,这意味着两个进程最终使用相同的队列。

因此实际上您的发布者正在发布消息,但随后发布者和订阅者正在争夺谁来处理它们。

当订阅者获胜时,您会看到您的预期行为。

当发布者获胜时,该消息没有处理程序,因此 NServiceBus 显示警告。这并不总是一个问题;在某些情况下,您可能希望接收并简单地忽略一条消息,但此警告至少让您知道它正在发生,并且在您的情况下,它表示该消息未被预期的应用程序处理。

所以要修复它,只需更改端点名称即可。 MySubscriber 和 MyPublisher,或类似的东西。

您甚至不需要使用该属性,您只需命名实现 IConfigureThisEndpoint 的类,NServiceBus 将基于它构造端点名称。您甚至可以使用下划线,例如 MyProject_MyPublisher : IConfigureThisEndpoint,NServiceBus 会将下划线转换为点,因此您将获得“MyProject.MyPublisher”的输入队列,当您有很多输入队列时,这对于命名空间非常有用端点跑来跑去。

关于c# - NServiceBus 警告 "No handlers could be found for message type",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12283490/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com