MassTransit中不正确的标头序列化

时间:2019-08-16 12:04:39

标签: rabbitmq masstransit

从[5.1.5] MassTransit更新为[5.5.4]后,标头中的标头序列化出现问题,标头中的数字变为字符串: 我这样添加标题:

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

[5.5.4]我得到:

enter image description here

[5.1.5]安装: enter image description here

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

UPD:[5.2.0]也有问题

UPD2:找到了对此问题有罪的提交: https://github.com/MassTransit/MassTransit/commit/e9209bc14f0ba30037d6766a0e0933e535ff3151

从这里开始使用“ SetTextHeader”功能,而不是设置所有标头。 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()));
        }
    }

但是我不明白-所有标题如何变成文本?由于我是通过(字符串,对象)重载来设置标头的。

1 个答案:

答案 0 :(得分:1)

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