

While it seems like this is maybe a problem that Microsoft should be fixing in Visual Studio, we’ve seen too many customers get burned by this. Visual Studio doesn’t bother to add the keyword that might save you. When you use Visual Studio’s tooling to implement the IHandleMessages interface, this is what you get: public Task Handle(PlaceOrder message, IMessageHandlerContext context) Consider applying the ‘await’ operator to the result of the call.īut for being so important, this is only a warning! Unless your project has been set up to treat all warnings as errors (and it probably should be), this code will still compile just fine.Įven though the async keyword will allow this situation to at least be detected, it’s far too easy to forget it for the simple reason that it’s Visual Studio’s fault. Warning CS2014: Because this call is not awaited, execution of the current method continues before the call is completed.

But now we want to publish an OrderPlaced event, so we add a line to do that: public Task Handle(PlaceOrder message, IMessageHandlerContext context)Ĭontext.Publish(new OrderPlaced ) ĭue to the addition of the async keyword on the method signature, this code generates the following compiler warning: Otherwise, you open yourself up to a world of pain.Ĭonsider this code: public Task Handle(PlaceOrder message, IMessageHandlerContext context) 🔗The problem with asyncĪwaiting an async method isn’t just a nice-to-have it’s mandatory. You should probably update to make sure you aren’t making this mistake in your code right now. So we’ve updated NServiceBus to include a new Roslyn analyzer, which ensures this is one thing you can’t screw up when using NServiceBus APIs. We’ve seen our customers make this mistake too many times (we’ve even done it ourselves!). Like the Rule of We Will Rock You, the Rule of Awaiting Tasks is unwritten, but the compiler is a bad DJ-it provides no support in making sure you follow through.

Calling a method that returns a Task must be awaited afterward. Queen’s “We Will Rock You” must be followed by “We Are The Champions.” Same with Led Zeppelin’s “Heartbreaker” -> “Living Loving Maid,” Van Halen’s “Eruption” -> “You Really Got Me,” and Boston’s “Foreplay” -> “Long Time.” You have to. The universe demands some things must always occur in a certain order.
