MassTransit 中的 headers 序列化不正确

Incorrect headers serialization in MassTransit

从 [5.1.5] MassTransit 更新到 [5.5.4] 后 headers 序列化出现问题 headers 中的数字变成字符串: 我这样添加 header:

_serviceBus.Publish(
            new TestMessage { TestLong = 2, TestString = "ol" },
            ctx =>
            {
                ctx.Headers.Set("SuperMarkerHeader", 1);
            }).ConfigureAwait(false);

[5.5.4] 我得到:

[5.1.5] 代表:

有人提供解决方案或相关信息吗?

UPD: [5.2.0] 也有问题

UPD2:找到问题的提交者: https://github.com/MassTransit/MassTransit/commit/e9209bc14f0ba30037d6766a0e0933e535ff3151

这就是开始使用 "SetTextHeader" 函数的地方,而不是设置所有 headers。 https://github.com/MassTransit/MassTransit/blame/e9209bc14f0ba30037d6766a0e0933e535ff3151/src/MassTransit.RabbitMqTransport/Transport/RabbitMqSendTransport.cs#L88

所以代码转换来自:

KeyValuePair<string, object>[] headers = context.Headers.GetAll()
    .Where(x => x.Value != null && (x.Value is string || x.Value.GetType().GetTypeInfo().IsValueType))
    .ToArray();

foreach (KeyValuePair<string, object> header in headers)
{
    if (properties.Headers.ContainsKey(header.Key))
        continue;

    properties.SetHeader(header.Key, header.Value);
}

收件人:

    foreach (var header in headers.GetAll())
    {
        if (header.Value == null)
            continue;

        if (dictionary.ContainsKey(header.Key))
            continue;

        if (header.Value is string stringValue)
        {
            dictionary[header.Key] = converter(header.Key, stringValue);
        }
        else if (header.Value is IFormattable formatValue && formatValue.GetType().IsValueType)
        {
            dictionary.Add(header.Key, converter(header.Key, formatValue.ToString()));
        }
    }

但我不明白 - header 怎么都变成文本了?因为我被设置为 header (string, object) overload.

这是在 5.20 中更改的,并非有意为之。 RabbitMQ 有线格式化程序支持已知类型,我将在下一个版本中恢复对这些类型的支持。我还添加了将 DateTime/DateTimeOffset 转换为 AMQP 时间戳(如果可能)的支持,否则,它将被格式化为字符串。