Proper use of async / wait in the web API

I have an existing business library that I want to open using the Web API. My existing business classes look like

public class Business
{
    public bool DoSomeBusiness()
    {
        //Performing long running  DB operations
        return true;
    }
    //Other methods
}

I am writing a Web API method, such as the following code, and use asyn / await for better scalability.

public class SampleController : ApiController
{
    Business _business;
    public ValuesController(Business business)
    {
       _business = business;
    }
    public async Task<HttpResponseMessage> Get()
    {
       var result= await Task.Run(() => _business.DoSomeBusiness());
       return Request.CreateResponse(HttpStatusCode.OK, result);
    }
}

Is this approach right? Will I really benefit from asynchronous behavior? I do not want to change the existing implementation of the business level to make them task-based.

+4
source share
1 answer

It does nothing. If so, ASP.NET can simply run your action in the Task.Run call automatically and achieve better scalability.

. async IO, . , , , .

, , :

fooobar.com/questions/264633/... EF 6 ? fooobar.com/questions/264635/... async I/O ?

+9

Source: https://habr.com/ru/post/1625635/


All Articles