VladOhotnikov
VladOhotnikov

Reputation: 1188

Incorrect headers serialization in MassTransit

I have problem with headers serialization after update from [5.1.5] MassTransit to [5.5.4] numbers in headers become strings: I add header like this:

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

[5.5.4] I get:

enter image description here

[5.1.5] Insted of: enter image description here

Have someone the solution or info about it?

UPD: [5.2.0] Have the problem too

UPD2: Found the commit, who is guilty in the problem: https://github.com/MassTransit/MassTransit/commit/e9209bc14f0ba30037d6766a0e0933e535ff3151

That's where started using "SetTextHeader" function, instead of set all headers. https://github.com/MassTransit/MassTransit/blame/e9209bc14f0ba30037d6766a0e0933e535ff3151/src/MassTransit.RabbitMqTransport/Transport/RabbitMqSendTransport.cs#L88

So code transofrm from:

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

To:

    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()));
        }
    }

But I can't understand - How all headers become text? Since I am set header by (string, object) overload.

Upvotes: 0

Views: 312

Answers (1)

Chris Patterson
Chris Patterson

Reputation: 33258

This was changed in 5.20, and it wasn't intentional. There are known types which are supported by the RabbitMQ wire formatter, and I'm restoring support for those in the next release. I'm also adding support to convert DateTime/DateTimeOffset to an AMQP timestamp (if possible), otherwise, it will be formatted as a string.

Upvotes: 1

Related Questions