Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Handler and Consumer in MassTransit

Tags:

masstransit

What is the difference between a Handler and a Consumer in MassTransit? I've seen examples that use one or the other, but conceptually, I'm unclear on what the difference is between the two and why you would want to choose one or the other.

like image 608
Remi Despres-Smyth Avatar asked Jun 30 '16 14:06

Remi Despres-Smyth


People also ask

How does MassTransit work?

By default, MassTransit uses JSON to serialize messages, while simultaneously suppporting the deserialization of JSON, BSON, and XML messages. Additional deserializers can be added, and the serializer can be changed for each receive endpoint or the entire bus.

What is bus in MassTransit?

MassTransit is a lightweight service bus for building distributed . NET applications. The main goal is to provide a consistent, . NET friendly abstraction over the message transport.


1 Answers

A Consumer is a type you register that has a specialized handler which handles the lifecycle of your Consumer object.

A handler is effectively just a generic event handler.

You would use a handler if just need a small Action<> block to resolve whatever action on your message. You would use a Consumer if you want to register a type, that has a lifecycle, to resolve whatever action on your message. Generally, you'll end up wanting a Consumer because want your container to resolve dependencies for the type instead of just having them in scope of your handler. But handlers work great for small things or request-response scenarios.

like image 171
Travis Avatar answered Nov 06 '22 13:11

Travis