MassTransit mediator and CorrelationId

36 Views Asked by At

I'm using MassTransit mediator (v7) to publish a bunch of in-process events in my application. A few events are initially published and the corresponding handlers publish in turn multiple events, etc.

I need to get the correlation ID within my different handlers so I can keep track of the overall progress and know when the operation is finished.

I've tested multiple approaches and so far I'm always getting a null CorrelationId in the ConsumeContext (except for the very first event)

I've started by adding a CorrelationId property to my events:

class CorrelatedTestEvent
{
    public Guid CorrelationId { get; set; }
}

class CorrelatedTestEvent2
{
    public Guid CorrelationId { get; set; }
}

class CorrelatedTestEvent3
{
    public Guid CorrelationId { get; set; }
}

I've also implemented CorrelatedBy<Guid> on my events

And I publish my events like so :

IMediator mediator;
...

var correlationId = Guid.NewGuid();
var @event = new CorrelatedTestEvent() { CorrelationId = correlationId } 

await this.mediator.Publish<TEvent>(@event, cancellationToken);

I want the CorrelationId to be set for all the handlers within the same operation. How can I achieve this? Is this even possible?

0

There are 0 best solutions below