#CLASSIC ROCK BLOG CODE#
You shouldn’t have to worry about whether your async code is correct.
#CLASSIC ROCK BLOG UPGRADE#
Check out the NServiceBus analyzer documentation, and then upgrade as soon as you can. NServiceBus 6.5 and NServiceBus 7.1 are available on NuGet now. That’s why we’re backporting this feature to NServiceBus 6, rather than just for NServiceBus 7. The production problems it causes are insidious and difficult to track down, yet they’re entirely preventable.
![classic rock blog classic rock blog](https://images.radio.com/aiu-media/bowieblog-ac155e4a-9f7b-4bea-9a9b-8ec1f04af418.jpg)
(I would not be one of those people.) But forgetting to await a Task definitely matters a whole lot. Some may say the order in which you listen to songs by Queen doesn’t matter. If so, great! We’ll happily remove the analyzer from NServiceBus at that time, content that we helped everyone to write better async code. One day Visual Studio may decide to fix this problem and require that tasks be assigned or awaited. If you’ve already updated to NServiceBus 7, it will be available in version 7.1. If you’re on NServiceBus 6, you can use the NServiceBus 6.5 package to get the analzyer. We wanted to make this analyzer available for all our customers, regardless of which version of NServiceBus you’re using. It will no longer be possible to forget to play “We Are The Champions” after “We Will Rock You.” The compiler will make sure that you do. If you forget, you’ll get this compile-time error:ĮRROR NSB0001: A Task returned by an NServiceBus method is not awaited or assigned to a variable. We’re now shipping a Roslyn analyzer directly in the NServiceBus package that will detect when you use one of our async API methods and you don’t immediately await or assign the task to a variable. With Roslyn analyzers, we have the tools to fix it and keep our users from experiencing this pain. 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.
![classic rock blog classic rock blog](https://i.pinimg.com/564x/b6/2d/83/b62d833e1fe5695571672ed4503dcc8c--golden-hair-classic-rock.jpg)
#CLASSIC ROCK BLOG UPDATE#
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.
![classic rock blog classic rock blog](https://i.pinimg.com/564x/a0/d6/b7/a0d6b7cc28f74b3469b3d4481d9a87ec--jethro-tull-rock-bands.jpg)
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.