async/await - when to return a Task vs void?

后端 未结 7 1159
梦如初夏
梦如初夏 2020-11-21 23:47

Under what scenarios would one want to use

public async Task AsyncMethod(int num)

instead of

public async void AsyncMetho         


        
7条回答
  •  再見小時候
    2020-11-22 00:05

    According to Microsoft documentation, should NEVER use async void

    Do not do this: The following example uses async void which makes the HTTP request complete when the first await is reached:

    • Which is ALWAYS a bad practice in ASP.NET Core apps.

    • Accesses the HttpResponse after the HTTP request is complete.

    • Crashes the process.

提交回复
热议问题