Search code examples
c#masstransit

How should be initialized CorrelationId at MassTransit


I'd like to track my messages using built-in interface CorrelatedBy<TKey>, but i didn't quite understand: should i initialize it myself, for example, in constructor of my message (command)?

public class RegisterCallback : IRegisterCallback
{
    public RegisterCallback()
    {
        CorrelationId = Guid.NewGuid();
    }

    public Guid RequestId { get; set; }
    public Guid CorrelationId { get; }
}

Solution

  • You should initialize it by either passing it into the constructor, or otherwise generating it as part of the constructor.

    public RegisterCallback(Guid correlationId) {...}
    

    Or you can generate it using NewId to get an ordered identifier.

    public RegisterCallback()
    {
        CorrelationId = NewId.NextGuid();
    }
    

    Also, your interface should include CorrelatedBy<Guid> if you want to use the built-in support.

    public interface IRegisterCallback :
        CorrelatedBy<Guid> {...}