Async expects a long synchronization call and asynchronous methods

I am using asp.net web api 2 and Entity Framework 6.

Original pseudo code

public IHttpActionResult GetProductLabel(int productId) { var productDetails = repository.GetProductDetails(productId); var label = labelCalculator.Render(productDetails); return Ok(label); } 

Modified code

 public async Task<IHttpActionResult> GetProductLabel(int productId) { var productDetails = await repository.GetProductDetailsAsync(productId); // 1 long second as this call goes into sub services var label = labelCalculator.Render(productDetails); // 1.5 seconds synchrounous code return Ok(label); } 

Before my change, everything was done synchronously.

After my change, a call to a remote service that calls the database again is asynchronous.

Then I make a synchronization call to a rendering library that offers only synchronization methods. The calculation takes 1.5 seconds.

Is there any other advantage in that the remote database_service service made an asynchronous call, but there was no second call? And is there anything that I could improve?

Note

The reason I'm asking about this is this:

"With asynchronous controllers, when a process waits for I / O to complete, its thread is freed up for use by the server to process other requests.

So, when the first remote database_service call is processed and waits 1 second, the thread returns to IIS ?? !!

But what about calculating the 2nd mark, which is 1.5 seconds, which again blocks the current thread in 1.5 seconds?

So, I release and block the flow, which does not make sense or what do you think?

+8
c # asynchronous async-await asp.net-web-api2
source share
3 answers

A rendering library is not just β€œblocking a stream”, it does the work of rendering. There is nothing better you can do.

+2
source share

Asynchronous code creates a certain continuation under the hood, it is a kind of synthetic sugar to make asynchronous programming more synchronous.

In general, depending on the operations themselves, this can help make both long-running tasks asynchronous. It will use different tasks under the hood for each individual task with a long lead time and run them asynchronously.

Currently, these tasks are performed completely synchronously in GetProductLabel, which means that if this is the only method you call, you would not indicate the difference between the synchronous code.

If possible, I would do the second async method, as I am not familiar with any serious drawbacks of using tasks and expecting async.

In your case, there is nothing better that you can do, and it will not make much difference, since you must run it synchronously, since you are using the result from the first method.

+1
source share

Is there another advantage that I made a remote call to database_service in an asynchronous way, but there is no second call?

Yes, this call is now not blocked and can work together with other code, although it is only for 1 second.

And is there anything that I could improve?

If you can start the second call asynchronously, the whole method can execute asynchronously and will not block at all.

+1
source share

All Articles