A Visual Studio extension that warn to configure an awaiter in await calls
We always forget the ConfigureAwait(...)
, so this extension warns us about this and fixes it! 😎
I made this project just for fun. I'm sure there are some edge cases that I didn't take into account
Meanwhile, enjoy it! 😉
I used the Visual Studio Extensibility Template Analyzer with Code Fix (.NET Standard). In theory, this extension can be deployed as either a NuGet package or a VSIX extension. It was tested using Microsoft Visual Studio 2017 Version 15.9.11
It only runs if there are no compilation errors.
It analyzes the AwaitExpressionSyntax
nodes and check if their expression (method call or variable) is of type ConfiguredTaskAwaitable
.
That's it.
It modifies the AwaitExpressionSyntax
's ExpressionNode
in order to add the .ConfigureAwait([true|false])
expression nodes:
this way we make the fix.
There are two code fixes: 'Add ConfigureAwait(false)
' and 'Add ConfigureAwait(true)
'
-
Check edge cases like:
Await an already configured task (invoked as variable) :var task = MethodAsync().ConfigureAwait(false); var result = await task; // <- this generates an unnecesary warning ~~~~~~~~~~~
Await calls inController
'sAction
inASP.NET Core
appsclass HomeController : Controller { public async Task<IActionAsync> GetData() { return Ok(await GetDataAsync()) // <- this generates an unnecesary warning ~~~~~~~~~~~~~~~~~~~~ } }
-
Maybe do something smarter than just check the suffix on thestring
representation -
Allow user settings to:
- activate/deactivate the extension
- change if the extension should report Warnings or Errors
- Creating a .NET Standard Roslyn Analyzer in Visual Studio 2017
- Starting to Develop Visual Studio Extensions
- How To Write a C# Analyzer and Code Fix
- Working with types in a Roslyn analyzer
If you want just the *.vsix
file download it from the releases
Coded by JJ - 2019
Thanks to @carlosbonillabirchman for telling me about this idea
Licensed under the MIT license