Asynchronous APIs bring great performance improvements. However they introduce new risks for bugs due to missing
await operators. Starting with versions 6.5 and 7.1, NServiceBus comes with a built-in Roslyn analyzer to detect missing awaits on NServiceBus APIs.
The analyzer points out missing
await operators when using asynchronous methods on the following classes and interfaces:
IUniformSession(from the UniformSession package)
await or assign the tasks returned by these methods results in the following compile-time error:
This analyzer inspects only the APIs contained on the classes and interfaces as described in the previous section. Other asynchronous NServiceBus APIs are not analyzed.
The analyzer checks for ignored
Tasks returned from the scanned methods. If the
Task is assigned to a variable, passed to another method, or has one of its members accessed, the analyzer will not evaluate this
Task's usage any further.
The analyzer is part of the NServiceBus NuGet package and will be automatically installed when referencing the NServiceBus package in a project.
The analyzer requires Visual Studio 2015 Update 2 or newer.
The analyzer also works when building a .NET Core application using
dotnet build commands, allowing the analyzer to highlight missing
await operators in other development environments.
The analyzer can be disabled in several ways. However, disabling the analyzer is not recommended as it highlights missing
await operators, which can cause duplicate messages, message loss, transaction issues, and more.
#pragma warning disable NSB0001 context.Send(message); #pragma warning restore NSB0001
element to the csproj file.
The C# compiler already contains a set of inspections which can warn about incorrect usage of
Task-based APIs. It is recommended to treat these warnings as errors to ensure they are not missed accidentally. This feature can be enabled by the project settings or by adding
csproj file directly.