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

NServiceBus Handler throwing metadata errors

将 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;
}

我以前遇到过 "could not find metadata" 问题,这是由于 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 告诉您没有为消息类型 "string" 注册消息元数据。因此无法构造消息并抛出异常。