By default message handling in MassTransit is done through Consumers. A consumer consumes one or more message types when configured on or connected to a receive endpoint. MassTransit includes many consumer types, including consumers, sagas, saga state machines, routing slip activities, handlers, and job consumers.
With the upcoming 8.1 release, an extra way to handle messages will be "Minimal Message Handlers". Instead of defining a separate Consumer class, a function is provided to handle a given message type, in the trend of the "minimal APIs" feature in ASP.NET Core.
To use this feature, you need to use the AddHandler
method on the IBusRegistrationConfigurator
:
Of course in this example there is not much happening. Let us extend the example above and log the incoming request message. To inject extra dependencies, we can specify them as extra parameters to the method:
Instead of passing the message directly, we can also use the ConsumeContext
to access the message metadata:
If you want to learn more about Minimal Message Handlers, check out this video: