AgodaAnalyzers
AgodaAnalyzers copied to clipboard
Add rule for detecting and mass fixing legacy blocking calls
Feature request
Type
- [x] - Enhancement - completely new feature
- [ ] - Improvement - make what we have better
Problem we are solving?
We are migrating a lot of legacy code from ASPNET 472 to Net6+
There was a big problem in Legacy framework where if you left the request thread you lost HttpContext and subsequently lost request lifetime objects in IoC containers as containers like autofac, simpleinjector, etc used to rely on HttpContext for this.
This cause a lot of code to be written in a blocking way to avoid problems.
In dotnet core this was solved, so as we are migrating code to new system we want to auto the fixing of the blocking code into correct aync/await.
There's several patterns we are finding in the old code so this may get quite large.
Lastly it maybe be justifiable to break this into a separate library/package for "Legacy Migration Helpers" as its not really needed for new code. We have options.
Examples
Here is 4 examples that we want to check for and refactor
using System.Threading.Tasks;
internal static class Class1
{
public static string Wrong()
{
return Delay1().Result;
}
async static Task<string> Delay1() { await Task.Delay (1000); return 1; }
}
async static Task<int> Delay1() { await Task.Delay (1000); return 1; }
internal class Class2
{
public string Wrong()
{
return Delay2().Result.ToString();
}
private async Task<int> Delay2() { await Task.Delay(1000); return 1; }
}
internal class Class3
{
public int Wrong(int x)
{
if(x>1)
{
while(x==1)
{
return Delay3().Result;
}
}
return Delay3().Result;
}
private async Task<int> Delay3() { await Task.Delay(1000); return 1; }
}
internal class Class4
{
public int Wrong(int x)
{
if (x > 1)
{
return Delay4().Result;
}
return 1;
}
private async Task<int> Delay4() { await Task.Delay(1000); return 1; }
}
In each example we would remove the .Result
then add a await
, then change the method to add async
and wrap the return parameter in Task<{param}>