Search code examples
asp.net-mvc-5asp.net-core-mvc

Difference between .net core and Mvc api behaviour


Are .net core mvc api by default Asynchronous and .net mvc by default synchronous


Solution

  • .Net and .Net Core controller actions can be either synchronous or asynchronous. The framework takes care of the detail behind awaiting the controller action or not.

    To make your controller action asynchronous, set the return type to be on of the Task, Task types.

    The framework will await the result and you can also add in a CancellationToken into the parameters of your action and it will be added for you by the framework for when a client disconnects so you can cancel any long running tasks effectively.

    Please note though, if you are hosting in IIS, to avoid app pool locking, you should use an async action if you are going to be using async code in a lower layer. Using .Wait or .GetResult will cause the app pool thread to look and your app pool will crash