12

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.

Remi Despres-Smyth
  • 4,173
  • 3
  • 36
  • 46

1 Answers1

14

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.

Travis
  • 10,444
  • 2
  • 28
  • 48
  • Given your description, then, I take it there will be a Consumer instance per message? If I set a fault handler on the same class, would the last instance be used when handling the fault, or would it be an entirely new instance? – Remi Despres-Smyth Jun 30 '16 at 16:01
  • 1
    Depends on registration. If you register with an IoC container helper, it lets the container maintain the lifecycle. Also consider than state will be troublesome to be shared across messages since generally there are multiple instances if it's a singleton -- at least one per thread. – Travis Jun 30 '16 at 16:03