简体   繁体   中英

Making async WebApi wrapper over existing code

We are exposing our current logic/business layer using WebAPI. As per my understanding, if we want to keep our self safe from thread starvation for the requests, we should make Async WebAPI controller, so a large number of concurrent request can make up.

I do understand that as the underlying service/business layer is synchronous, so there will be no performance gain. We are just aiming for a large number of concurrent requests to pass through.

Below is the code that i am using:

public async Task<IHttpActionResult> Get()
{
     var result = await Task.Run(() => Service.GetAllCompanies()); //existing business layer
     return Ok(result);
}

Wrapping underlying layer in a Task, is this good to proceed with and achieve the goal.

My understanding is that if your await -ed methods' implementations aren't async, then you are not really accomplishing what you think you are. If you have CPU-bound service methods, you're just releasing a thread from the request-handling pool and then spinning up a new one (from the same pool, mind you) when you Task.Run(); So you incur some overhead from that switch, but the ASP.NET thread pool is still doing the work, so you haven't achieved what you want.

If you service methods can be converted to be pure (or mostly pure) async code, then you would stand to benefit from working bottom up.

Reference: https://msdn.microsoft.com/en-us/magazine/dn802603.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM