'MassTransit Create Request with Metadata

I use MassTransit package into my project (clean Architecture).

In API-layer I write custom authorization filter and in HttpContext exist my CurrentUserInfo data.

then I Need UserInfo inside the Application Layer where IConsumer exist like Code below.

Now which way is better to do:

  • add metadata into my request client?
  • declare general class and inject it into implemented IConsumer class.
public class OptionCreateConsumer : IConsumer<OptionCreateRequest>
{
    private readonly IMapper _mapper;
    private readonly IUnitOfWork _unitOfWork;

    public OptionCreateConsumer(IMapper mapper, IUnitOfWork unitOfWork)
    {
        _mapper = mapper;
        _unitOfWork = unitOfWork;
    }
}


Solution 1:[1]

In the scoped filter sample, you can see how Send/Publish and Consume filters are used to capture and pass a Token object from the request client to the consumer.

The TokenActionFilter is added so that it is scoped as well, pulling the incoming data into the Token (which in your case, would be the UserInfo object.

The filters configured for MassTransit are used to add that data to the headers, so that downstream consumers are able to use that header to initialize the Token (or UserInfo for your scenario).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Chris Patterson