0

I am upgrading NServiceBus from an older version to version 7. Half of the code was already upgraded by an earlier developer. The project is using a mediator pattern and there are lots of handlers in one subscriber project. Most of the handlers are async.

These handlers perform Database updates and some other handlers perform database updates and web service calls.

Inside the handlers there are lots of sync methods that do database operations and web service calls.

Since the handlers are both async and sync, will I face any issues if I move to production with this code?

Wyck
  • 10,311
  • 6
  • 39
  • 60
  • There is nothing wrong using both or mix of async and sync handlers. It all depends on the requirement. – Swinkaran Feb 26 '20 at 23:11

1 Answers1

0

The handlers will have to return a Task due to the fact that NServiceBus IHandleMessages<T> mandates handlers to do so (public Task Handle(T message, IMessageHandlerContext context). Wherever you mark it with async and use the await keyword will depend on what you are doing. If you're going to invoke anything that is an asynchronous operation (sending or publishing using NSreviceBus, 3rd party asynchronous code) it is better to maximize the benefits of asynchronous APIs by having database operations use async variants.

It's OK to mix asynchronous and non-asynchronous, just don't forget to await any async operation.

Francesc Castells
  • 2,692
  • 21
  • 25
Sean Feldman
  • 23,443
  • 7
  • 55
  • 80