Async method with Await on his return [duplicate]

1
public async Task<IHttpActionResult> NomeMetodo([FromUri] Filtro filtro)
{
    return await Task.Factory.StartNew(() =>Ok(_aplicacao.RetornarDados(filtro)));
}

I would like to know why the above method makes sense, since it is async but I'm expecting the return of it in await , which to me looks like a normal sync method. I do not handle a lot of asyncs, could you explain me?

    
asked by anonymous 18.04.2016 / 15:42

1 answer

2

In this case it really does not make sense to use async / await . Asynchronism is a way to tell a Thread that a await operation this Thread does not have to wait for await to process other information. This means that Thread that would be idle during a certain operation in this case will be free to process further information until the await operation is not completed.

Only operations with asynchrony only make sense when you are working with third-party resources, where processing will not be performed by the same resource that is used to run your code. A very simple example of this is when we consume third-party libraries via the HTTP protocol. Here is a (simplified) example taken from MSDN :

async Task<int> AccessTheWebAsync()
{
    HttpClient client = new HttpClient();

    DoIndependentWork();

    string urlContents = await client.GetStringAsync("http://msdn.microsoft.com");

    return urlContents.Length;
}

Note that when we call the GetStringAsync method here we are using the word await , stating that this is an asynchronous operation. In this context it makes sense to use asynchronism, we are making a call to an external service, where processing is not effected by the same resources that manage our code, where the resolution time of the method can be several minutes because of bottlenecks that the site is facing or may even occur some problem. If the processing is being done by another resource that is not mine, why should I lock the resources of a Thread , and could my resources be free to solve other problems?

A more humane example for understanding:

We have a cook who is preparing an omelet. The cook has to perform various tasks to prepare the omelet, such as breaking the eggs, stirring the eggs, putting salt inside the scrambled eggs and finally, put the eggs in a frying pan. Only that our cook decided he would put melted cheese on top of the omelet and would use a microwave for it. See that all the actions of breaking the egg, moving the eggs and putting salt inside the eggs requires the cook to use his (his hands) resources to perform these actions, microwave task, you just need to get the cheese (parameters) . Obviously the cook needs to wait for the melted cheese to put it on top of the eggs and serve, but does he have to stand in front of the microwave waiting for the cheese to be melted? Meanwhile, he could start preparing other dishes while the cheese is being melted.

Why the example put in the question does not make sense?

In the code posted in the example there is no action that is carried out by third parties and which demands asynchronism. Asynchronism should most likely be in the ReturnData method, but it does not in fact have an asynchronous return. This could be corrected as follows:

public async Task<IHttpActionResult> NomeMetodo([FromUri] Filtro filtro)
{
    var dados = await _aplicacao.RetornarDadosAsync(filtro);
    return Ok(dados);
}

Note that the Async suffix is a good practice for reporting that the method has an asynchronous return, following a great programming principle: Principle of least surprise.

I believe this method must be performing an operation on a database. Note that a third-party feature may even be a feature of your own machine. I / O operations are examples of a use of asynchronism where the third party resource is performed by its own machine.

    
18.04.2016 / 18:15