0

I've asp.net web API integrated with public ui web site

the website will have many concurrent users that submit applications , api pull this to database.

the API do a lot functions like :

  • Get Data

  • Update Data

  • Upload files

I'm trying to use Async operations ( specially in upload documents )

I edited all function to use await Task.Run()=>bla())) , like this :

if (Someconditions)
{
await Task.Run(() => Function1());
await Task.Run(() => Function2())
if (Someconditions)
await Task.Run(() => Function3())

if (Someconditions)
await Task.Run(() => CreateUpdateDocumentDetails)); // Uploading document

For document upload function , I'm very confused

Should I use await UploadDocumentAsync() or

Task.Run(()=>UploadDocumetSync()) or Task.Run(()=>UploadDocumentAsync())

Also Does Task.Run(SyncFunc()) mean all above function runs asynchronously without marked them as async and is it good practice?

Last questions what's the difference between this and making all functions (async / await)

1
  • It's usually a bad sign of poor design when you are calling Task.Run() inside an web API request handler -- especially when using a conventional controller or request handler shipped with ASP.NET Core. Commented Oct 22, 2018 at 10:34

1 Answer 1

0

1 .It's an uploading process better is Async

2 .Use Task.Run(()=>UploadDocumentAsync());

3 .Async tells caller, I am an asynchronous method, don't wait for me.await inside the Task() ask for waiting for an asynchronous task.

for example after running below program will wait 10 seconds to show the task done.

public static void Main(string[] args){
     Task();
     System.Console.WriteLine("main thread is running while waiting");
     Console.ReadLine();
}
private static async void Task(){
     await Task.Delay(10000);
     Console.WriteLine("Waited for 10s");
}
Sign up to request clarification or add additional context in comments.

1 Comment

thank you for clarification , what happens if use Task.Run()=>SyncMethod()) , will this occurs async or as the original function is sync , it will run sync and ignore task

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.