How to write asynchronous functions, that will nicely integrate in .NET Core 3.1 and ASP.NET Core applications?

  • Thread starter Thread starter burnersk
  • Start date Start date
B

burnersk

Guest
In the past time, I have optimized my functions by using multiple tasks to do stuff in parallel and have waited until all tasks had been finished.

Now, entering the ASP.NET Core 3.1 world, I need to use the async concept, as the web server would block other requests until the previous request has finished.
But I do not want to cook something just for ASP.NET Core, I want to have it universal (.NET Core 3.1 desktop applications and ASP.NET Core 3.1 web applications).

Here is a sample code, I am using now for asynchronous operations:

public async void DoSomethingLongLasting()
{
ProjectFile project = await new Task<ProjectFile>(() => { return new ProjectFile(); });
await new Task(() => { project.MasterKey = @"<RSAKeyValue><Modulus>0123456789ABCDEF</Modulus><Exponent>AAAA</Exponent><P>0123456789ABCDEF</P><Q>0123456789ABCDEF</Q><DP>0123456789ABCDEF</DP><DQ>0123456789ABCDEF</DQ><InverseQ>0123456789ABCDEF</InverseQ><D>0123456789ABCDEF</D></RSAKeyValue>"; });
Guid someGuid = await new Task<Guid>(() => { return Guid.NewGuid(); });
Guid emptyGuid = Guid.Empty;
}

The statements with the await keyword are statements, that will take long time to complete. The order is important (despite the Guid generation).

Is this approach usable? Does it have drawbacks or pitfalls? Are there other ways?

Continue reading...
 
Back
Top