Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rule for detecting and mass fixing legacy blocking calls #174

Open
1 of 2 tasks
dicko2 opened this issue May 9, 2023 · 0 comments
Open
1 of 2 tasks

Add rule for detecting and mass fixing legacy blocking calls #174

dicko2 opened this issue May 9, 2023 · 0 comments

Comments

@dicko2
Copy link
Contributor

dicko2 commented May 9, 2023

Feature request

Type

  • - 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}>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant