async/await in MVC controller's action

后端 未结 3 1725
遇见更好的自我
遇见更好的自我 2021-02-07 12:35

I have an Index action in ASP.net MVC controller. This action, calls (among other things) a private action that do a count on a SQL table with large set of rows. Th

3条回答
  •  独厮守ぢ
    2021-02-07 13:04

    Instead of async, use Task.Run:

    Task.Run(() => MethodToExecuteInBackground(methodParameters)).ConfigureAwait(false).GetAwaiter();
    

    You will also need to remove the async modifier from your controller Index method.

    Now your controller method will return immediately while MethodToExecuteInBackground goes off and does some work in the background.

    Be aware that this will tie up a thread-pool thread until MethodToExecuteInBackground finishes.

提交回复
热议问题