gpt4 book ai didi

c# - NServiceBus 处理程序抛出元数据错误

转载 作者:太空宇宙 更新时间:2023-11-03 23:09:05 31 4
gpt4 key购买 nike

将 NServiceBus 更新到上周发布的最新版本(也有新的实现)后,我在从客户端发送 json 时看到奇怪的错误。

我将从客户端发送消息,接收方显示此消息:

2016-10-18 22:16:33.612 INFO MFG.Receiver.DeviceHandler Got message with id: a222b136-6a4e-474e-8012-cc1c24e5e539

我的处理程序中有一个断点,如下所示,它显示消息对象已烘焙,应该不会有任何问题。

public class DeviceHandler : IHandleMessages<DeviceRequest>
{
private readonly IDeviceProvider _provider = new DeviceProvider();
private static readonly ILog Log = LogManager.GetLogger<DeviceHandler>();

public Task Handle(DeviceRequest message, IMessageHandlerContext context)
{
Log.Info($"Got message with id: {context.MessageId}");
...
return context.SendLocal($"Message with Id {context.MessageId} received.");
}
}

当它最后遇到 reply 方法时,会抛出以下错误:

2016-10-18 22:16:33.666 INFO NServiceBus.RecoverabilityExecutor Immediate Retry is going to retry message 'a222b136-6a4e-474e-8012-cc1c24e5e539' because of an exception: System.Exception: Could not find metadata for 'System.String'. Ensure the following: 1. 'System.String' is included in initial scanning. 2. 'System.String' implements either 'IMessage', 'IEvent' or 'ICommand' or alternatively, if you don't want to implement an interface, you can use 'Unobtrusive Mode'. at NServiceBus.Unicast.Messages.MessageMetadataRegistry.GetMessageMetadata(Type messageType) in C:\Build\src\NServiceBus.Core\Unicast\Messages\MessageMetadataRegistry.cs:line 39

我不确定为什么它会抛出 System.String 错误,在它已经收到来自处理程序的消息并且属性已填充之后......

发送的 json 看起来像这样:

{
"$type": "DeviceRequest, MFG.Domain",
"Id": "devices-65",
"DeviceId": 1,
"Location": "Orlando",
"DeviceType": "test"
}

我的发件人(客户端)看起来像这样:

static void Main()
{
...
using (var channel = connection.CreateModel())
{
var messageId = Guid.NewGuid().ToString();
var properties = channel.CreateBasicProperties();
properties.MessageId = messageId;

var payload = GenerateJsonPayload();

channel.BasicPublish(string.Empty, ServerEndpointName, false, properties, Encoding.UTF8.GetBytes(payload));
Console.WriteLine($"Message with id {messageId} sent to queue.");
}
...
}

public static string GenerateJsonPayload()
{
var obj = new DeviceRequest
{
DeviceId = 1,
DeviceType = "test",
Location = "Orlando"
};

var settings = new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.All
};

var result = JsonConvert.SerializeObject(obj, Formatting.Indented, settings);

return result;
}

我之前遇到过“找不到元数据”的问题,这是由于 json 格式错误或没有类型。如果我删除了 JsonSerializerSettings,并且只是传递了一个序列化对象,我反而会得到错误:

2016-10-18 22:31:27.698 ERROR NServiceBus.RecoverabilityExecutor Moving message '6405179d-ea36-4264-af2a-704da19af120' to the error queue 'error' because processing failed due to an exception: NServiceBus.MessageDeserializationException: An error occurred while attempting to extract logical messages from transport message 6405179d-ea36-4264-af2a-704da19af120 ---> System.Exception: Could not find metadata for 'Newtonsoft.Json.Linq.JObject'.

我不知道我在这里遗漏了什么,这不是以前版本的问题。这是错误还是...?

最佳答案

为您的 SendLocal 操作使用具体的消息类型。

作为处理消息的一部分,您正在执行 return context.SendLocal($"Message with Id {context.MessageId} received.");。这将尝试将类型为“string”的消息发送到本地队列。 NServiceBus 告诉您没有为消息类型“字符串”注册消息元数据。因此无法构造消息并抛出异常。

关于c# - NServiceBus 处理程序抛出元数据错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40121175/

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