Changing my server-side WebAPI to get the benefits of async/await
asp.net-web-api, async-await, c#
Solution
If what you want to achieve is to free threads, the second example is better. The first one holds a thread throughout the operation.
Explanation
All that `async-await` does is to simply helps you write code that runs asynchronously but "looks" synchronous.
There are 2 reasons for asynchronous code:
- Offloading. Used mostly in `GUI` threads, or other "more important" threads". (Releasing threads while waiting for CPU operations to complete).
- Scalability. Used mainly in the server-side to reduce resource usage. (Releasing threads while waiting for IO to complete).
Unsurprisingly the reasons match your examples.
In the first example you run `DoThingsAndGetResults` on a different thread (using `Task.Run`) and waiting for it to complete asynchronously.The first thread is being released, but the second one doesn't.
In the second one you only use 1 thread at a time, and you release it while waiting for IO (Entity Framework).
Problem
Trying to understand server-side async/await use. My understanding is that it is only useful when the thread can be freed. If I have: ``` [HttpGet] public async Task<IEnumerable<MyObject>> Get() { return await Task<IEnumerable<MyObject>>.Run(() => DoThingsAndGetResults()); } IEnumerable<MyObject> DoThingsAndGetResults() { // ...Do some CPU computations here... IEnumerable<MyObject> result = myDb.table.Select().ToList(); // entity framework // ...Do some CPU computations here... return result; } ``` Will the thread ever be freed? Is Async/Await useless in this case? Is the only way to gain benefit from async/await is if I make some bigger changes and do this: ``` [HttpGet] public async Task<IEnumerable<MyObject>> Get() { return await DoThingsAndGetResultsAsync(); } async IEnumerable<MyObject> DoThingsAndGetResultsAsync() { // ...Do some CPU computations here... IEnumerable<MyObject> result = await myDb.table.Select().ToListAsync(); // entity framework // ...Do some CPU computations here... return result; } ```