MassTransit offers a lot of flexibility and it is easy to tweak it to your needs.
With all this power it is not always easy to find out what is the best way to solve a specific problem. I wanted to include a header with information about the user in every message.
I solved it by adding two filters; one for sending messages and one for publishing messages:
Inside these filters I resolved an IUserFactory which gave me access to the underlying user. This is how I implemented this in ASP.NET Core:
I registered both filters in the IoC container(Autofac in our case) to make sure that the dependencies are correctly resolved:
And as a last step I configured MassTransit to use these filters: