NServiceBus icon indicating copy to clipboard operation
NServiceBus copied to clipboard

It's not always obvious that context should be used for sending/publishing instead of IMessageSession

Open kbaley opened this issue 3 years ago • 0 comments

This issue was transferred from a private repository

Users are able to use IMessageSession in the pipeline which can have side effects as outlined by the nservicebus documentation.

Some users make the error that they do not use the context argument for sending and publishing but use IMessageSession. This is more likely to happen once they are going to call other objects from handlers like invoking 'services' or 'repositories' via dependency injection.

The cause is that they didn't pass the context argument through all their objects. From the users' perspective, this is polluting the call graph for no obvious reason which makes them use IMessageSession. Unfortunately, there is no exception if this interface is used inside an incoming message context while it can have disastrous effects like ghost messages.

For customers that were used to IBus this behavior seems like something that should work and using IMessageSession does work but they are not aware that this is disconnected from the incoming handler context.

Eventually, Uniform Session was created, however, this is not the default and there are plenty of cases where this error was made and we guide people to start using Uniform Session. There are likely many users that don't even know they are using IMessageSession incorrectly.

Suggested solutions:

  1. Making the UniformSession behavior the default will result in a more robust messaging solution that prevents incorrect usage.
  2. Detect usage of IMessageSession in an incoming handler context and not allow sends/publishes unless messages explicitly have "immediate dispatch" set.

kbaley avatar Jul 22 '22 21:07 kbaley