Asp.net Web API | Async await or Task.Run RRS feed

  • Question

  • User370603270 posted

    Hello Guys,

    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)   


    Saturday, October 20, 2018 9:33 AM

All replies

  • User1120430333 posted

    So what about the database that will have many users using it concurrently  and record lockout conditions in the database tables due to concurrent usage?

    Saturday, October 20, 2018 12:12 PM
  • User-474980206 posted
    Using an async method is better than starting a new thread just to run an sync operation as async. There is a lot less overhead.
    Saturday, October 20, 2018 3:51 PM