【问题标题】:NServicebus receive messages without all the NServicebus specific stuffNServicebus 接收没有所有 NServicebus 特定内容的消息
【发布时间】:2022-08-19 23:15:44
【问题描述】:

我是 NServicebus 的新手,一直在努力在文档中找到答案。

我正在尝试接收一条以简单 JSON 格式发布到 Amazon SQS 的消息,如下所示:

\"MyMessage\": {
    \"Id\": 1,
    \"Name\": \"Name\",
    \"Field1\": \"text\",
    \"Field2\": 1,
    \"Field3\": false
}

但是,每当这被发送到队列时,我的 NServicebus 订阅者都会说这是一条有毒消息,并且不会尝试处理它。

我意识到这条消息缺少很多 NServicebus 的东西,因为当我通过 NServicebus 发布消息时,它看起来像这样:

{
    \"Headers\": {
        \"NServiceBus.MessageId\": \"a244a014-e331-41e6-b6ca-aed6011af905\",
        \"NServiceBus.MessageIntent\": \"Publish\",
        \"NServiceBus.ConversationId\": \"e42f0308-4c51-4787-ade0-aed6011af90f\",
        \"NServiceBus.CorrelationId\": \"a244a014-e331-41e6-b6ca-aed6011af905\",
        \"NServiceBus.OriginatingMachine\": \"DESKTOP-1234567\",
        \"NServiceBus.OriginatingEndpoint\": \"endpoint\",
        \"$.diagnostics.originating.hostid\": \"da7dce712dfbc0f093aa30eb7f25d2b4\",
        \"NServiceBus.ContentType\": \"application/json\",
        \"NServiceBus.EnclosedMessageTypes\": \"Type\",
        \"NServiceBus.Version\": \"7.7.3\",
        \"NServiceBus.TimeSent\": \"2022-07-18 17:10:16:400164 Z\"
    },
    \"Body\": \"Base 64 encoded string here\",
    \"S3BodyKey\": null
}

问题是我收到的消息不是通过 NServicebus 发布的,而是我上面显示的格式。它没有所有的标头和 base64 编码的正文。

有没有办法设置 NServicebus 能够接收和处理这样的消息?或者它只是不是为了处理这样的事情而设计的?

注意:这是一个 .Net 6 应用程序

编辑:我发现这篇文章提到了 NServicebus 如何在没有所有标题的情况下接收消息,但它没有提到如何。

https://www.bradjolicoeur.com/Article/nsb-features-message-headers

    标签: c# .net-6.0 nservicebus


    【解决方案1】:

    您想要的称为 Native Send,实际上是 documented。您必须使您的消息符合 NServiceBus 期望的格式,以便能够让处理程序正确处理它。

    本机发送函数如下所示:

    public static async Task SendMessage(IAmazonSQS sqsClient, string queue, string messageBody, Dictionary<string, string> headers)
    {
        var bodyBytes = Encoding.UTF8.GetBytes(messageBody);
        var base64Body = Convert.ToBase64String(bodyBytes);
        var serializedMessage = Newtonsoft.Json.JsonConvert.SerializeObject(new
        {
            Headers = headers,
            Body = base64Body,
        });
        var queueUrlResponse = await sqsClient.GetQueueUrlAsync(QueueNameHelper.GetSqsQueueName(queue));
        await sqsClient.SendMessageAsync(queueUrlResponse.QueueUrl, serializedMessage);
    }
    

    要使用它,您需要指定消息类型和其他一些标头值:

    await SendMessage(
        sqsClient: client,
        queue: "samples-sqs-nativeintegration",
        messageBody: "{Property:'PropertyValue'}",
        headers: new Dictionary<string, string>
        {
            {"NServiceBus.EnclosedMessageTypes", "MessageTypeToSend"},
            {"NServiceBus.MessageId", "99C7320B-A645-4C74-95E8-857EAB98F4F9"}
        }
    );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-15
      • 2013-10-14
      • 1970-01-01
      相关资源
      最近更新 更多