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                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="AdaskoTheBeAsT.Asyncify" Version="0.9.7.12">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AdaskoTheBeAsT.Asyncify --version 0.9.7.12                
#r "nuget: AdaskoTheBeAsT.Asyncify, 0.9.7.12"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// 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 either Task or Task<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.

There are no supported framework assets in this package.

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.

Version Downloads Last updated
0.9.7.12 110 12/8/2024
0.9.7.11 2,849 8/18/2024
0.9.7.10 319 7/23/2024
0.9.7.4 1,233 5/27/2024
0.9.7.3 2,414 3/5/2024
0.9.7.2 862 2/11/2024
0.9.7.1 187 2/5/2024

--- 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.