Prevent message handler called twice

Hello.

I ran into the following problem. I have base message DebitAccount. I have created DepositMoney that inherits from DebitAccount.

The problem is that the handler gets called twice. How can i prevent it from being called twice?

@SeanFeldman @danielmarbach

Looking at diagnostics it has the following:

        "CustomerService.Command.ApplicationService.Accounts.DebitAccountHandler",
        "CustomerService.Command.ApplicationService.Accounts.DepositMoneyHandler"
      ],

I want DepositCustomerMoney to only be handled by DepositMoneyHandler. How can I fix that issue?

Thanks

Hi @ekjuanrejon,

if DepositMoney inherits from DebitAccount the behavior you’re observing is the expected one: an endpoint containing a message handler for the base type and for the derived type will invoke the base type handler for all the incoming messages matching that type. In your scenario, given that DepositMoney inherits from a base type when handled the handler for the base type will be invoked too.

Before thinking about a solution, let me ask a couple of questions:

  • Why the two handlers are in the same endpoint?
  • What was the reason to create a derived message?

Cheers,
.m

@mauroservienti I want to disable that feature. The reason I have base command is to use generic in my domain layer. I don’t want the handler to be execute multiple times

Because there are time…I just want to debit and account and trigger and event. But when it is a deposit I want to trigger a fine grain event for other systems to react. Right now DepositMoney execute both handler and customer account money is doubled

Thanks for the details. That’s the default inheritance behavior, and there is no way to disable it. There are a couple of solutions:

  • the one I prefer is to not use inheritance, which doesn’t sound what you’re looking for, with messages. When it comes to messaging inheritance is mostly (if not only) used for versioning purposes
  • A ugly hack that skips the processing of the base message handler if it’s a derived type, e.g.:

//in the base message handler

if(message.GetType() is typeof(DerivedType))
{
   //skips the execution of this handler
   return;
}