Avoid returning the type of Task <Object> from the async function when using HttpClient

I am making HTTP calls using System.Net.Http.HttpClient . It seems that all calls should be asynchronous.

Let's say I have a project structure: MVC Web Application β†’ Business Layer β†’ Data Layer

In the data layer, I make HTTP web API calls to return the data, and I end up using this function:

 public async Task<IList<Product>> GetProducts() { HttpResponseMessage response = await client.GetAsync("api/products"); string data = await response.Content.ReadAsStringAsync(); IList<Product> products = JsonConvert.DeserializeObject<IList<Product>>(data); return products; } 

this then goes to BusinessLayer:

 public Task<IList<Product>> GetProducts(string name = null) { return _repository.GetProducts(name); } 

Then, finally, in the MVC controller:

 public IActionResult Index() { Task<IList<Product>> ProductsTask = _manager.GetProducts(); IList<Product> ProductsNonTask = products.Result.ToList(); return View(); } 

Should I make each function return a list of Task<IList<Product>> types leading to my MVC controller? As you can see in the MVC, I must first retrieve the products first with the help of a task wrapped around them. The debugger looks strange when I look through the list of products through it. So, as you can see, I am converting them into a regular list of products.

I am wondering if this is the right thing to do so that all my functions return the type Task<IList<Product>> up to my MVC controller or if there is a workaround so that my functions can still return to the standard product list, but continue to use async features for HttpClient ?

UPDATE: something is wrong with this:

 public IList<Product> GetProducts() { Task<HttpResponseMessage> response = client.GetAsync("api/products"); if (response.Result.IsSuccessStatusCode) { string data = response.Result.Content.ReadAsStringAsync().Result; IList<Product> products = JsonConvert.DeserializeObject<IList<Product>>(data); retVal = products; } } 
+5
source share
2 answers

If you want the operation to be virtually asynchronous, it must be asynchronous to the end.

The moment you block Task with Wait or Result , it is no longer asynchronous.

So no. Either use async fully or do not use it at all.

Interesting ... if there is a workaround, so my functions can still return the standard list of products, but continue to use async features for HttpClient?

No, really not.

You should probably just make your code asynchronous, as it has many advantages in scalability and performance. But if you are against this, you can use WebClient , which has synchronous web operations.

+7
source

Should I make each function return a list of Task> types leading to my MVC controller?

Yes, you need it, and you don’t even catch all the places, because Index itself must also be asynchronous.

Async is really contagious. Use if necessary, not the default.

+1
source

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


All Articles