Dan
Dan

Reputation: 5972

MassTransit publishing custom properties for Azure Service Bus

Does anyone know how I can publish ApplicationProperties (aka custom properties) to Azure Service Bus via the MassTransit Publish?

In the native ASB client library, we can do this...

var message = new ServiceBusMessage(msg);
message.ApplicationProperties.Add("foo", "bar");

But I can't see how to do that in MassTransit. Looking at the overloads for the Publish method, the closest thing I can see is the headers - ie...

await _bus.Publish(new SomeEvent { SomeProperty = "some-value" },
    context =>
    {
        context.Headers.Set("MyCustomProperty", "my-custom-value");
    },
cancellationToken);

UPDATE: I wrote the above snippet, as an example of what I'm doing - but it wasn't an exact copy. It turns out after seeing Chris's answer - that the above snippet does work - but in my actual code, "some-value" was actually a boolean - which is what the issue was!

But that just adds headers to the MT message envelope - not ASB custom properties.

Ie. in my first example, this is where I see the field in Azure Service Bus explorer...

enter image description here

Upvotes: 1

Views: 1047

Answers (1)

Chris Patterson
Chris Patterson

Reputation: 33278

If the header is a string, or can be rendered as a string using a the built in IFormatter type, it should be copied to the ApplicationProperties collection.

For instance, using:

await endpoint.Send(new PingMessage(), context =>
{
    context.Headers.Set("Easy-Header", "Easy-Value");
});

Produces the header as shown:

enter image description here

Upvotes: 1

Related Questions