Search code examples
c#.netpublishmasstransit

masstransit publish with context


How to pass custom value to MessageId when publish message ?

My topic endpoint configuration

cfg.Message<OrderSubmitted>(configTopology =>
{
     configTopology.SetEntityName("my-topic");

});

Somewhere in service I publish message like this

await azureServiceBus.Publish<OrderSubmitted>(contract);

How I can provide custom MessageId while publishing ? (I need this because I want to use ASB duplicate message detection)

I looked into source code of MassTransit and it seems like there is an overload method where I can pass IPipe<PublishContext<T>> publishPipe as 2nd parameter but I don't understand but question is how to create this object properly ?


Solution

  • You can set any of the envelope properties and message headers by specifying a callback with Send or Publish.

    await bus.Publish<OrderSubmitted>(message, x =>
    {
        x.MessageId = someGuid;
    });