AdaskoTheBeAsT.Asyncify
0.9.7.12
dotnet add package AdaskoTheBeAsT.Asyncify --version 0.9.7.12
NuGet\Install-Package AdaskoTheBeAsT.Asyncify -Version 0.9.7.12
<PackageReference Include="AdaskoTheBeAsT.Asyncify" Version="0.9.7.12"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
paket add AdaskoTheBeAsT.Asyncify --version 0.9.7.12
#r "nuget: AdaskoTheBeAsT.Asyncify, 0.9.7.12"
// Install AdaskoTheBeAsT.Asyncify as a Cake Addin #addin nuget:?package=AdaskoTheBeAsT.Asyncify&version=0.9.7.12 // Install AdaskoTheBeAsT.Asyncify as a Cake Tool #tool nuget:?package=AdaskoTheBeAsT.Asyncify&version=0.9.7.12
Asyncify-CSharp
Asyncify-CSharp is an analyzer and codefix that allows you to quickly update your code to use the Task Asynchronous Programming model. This model, introduced in C# 5, adds an intuitive way of handling asynchronous calls within C#.
The analyzer allows large codebases to be easily modified to use the TAP model by finding violations and applying fixes up the call tree.
Analyzer
The analyzer will throw warnings on use cases like below:
public void AnotherMethod()
{
var result = Test();
}
public int Test()
{
var result = AsyncMethod().Result; // Warning
var awaitable = AsyncMethod();
var result2 = awaitable.Result.ToString(); // Warning
(new int[0]).Select(x => AsyncMethod().Result); // Warning
return 0;
}
public async Task<int> AsyncMethod()
{
await Task.Delay(10);
return 0;
}
Code Fix
The code fix will fix the following things.
- Remove the call to
.Result
- Wrap it in an
await
expression (potentially wrapping in parentheses - Update the method signature to become
async
and eitherTask
orTask<T>
- Recursively find calls to this method and refactor those to use
await
and update their signature.
So the given code sample above becomes:
public async Task AnotherMethod()
{
var result = await Test();
}
public async Task<int> Test()
{
var result = await AsyncMethod();
var awaitable = AsyncMethod();
var result2 = (await awaitable).ToString();
(new int[0]).Select(async x => await AsyncMethod());
return 0;
}
public async Task<int> AsyncMethod()
{
await Task.Delay(10);
return 0;
}
Conditions
A violation will not be thrown if a refactoring is not possible if:
- The violation is within a lock statement
- The method contains out or ref parameters
Caution
The model of analyzers in Visual Studio is constrained to a single project. As the analyzers hook into the compilation, they are only capable of refactoring within a project.
However, calls from outside the project can be suffixed with .Result
causing a new violation in that project to which the code fix can be applied.
What about VB?
Currently only C# is supported, mostly because I only have tests running on C#. Also, one thing that is specific to C# is the refactoring of the return type.
Learn more about Target Frameworks and .NET Standard.
This package has no dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
--- 0.9.7
Added interface refactoring
--- 0.9.6
Bugfix for invocation not calling .Result
Bugfix for .Result calls outside of a method
Bugfix for variable access within a Lambda expression
--- 0.9.5707.38527
First version of the nuget package.