?
Solved

Async and await with MVC and Web Api

Posted on 2016-10-11
3
Medium Priority
?
63 Views
Last Modified: 2016-10-11
Please see the following code. I want to make sure I'm implementing async and await correctly. This concept is new to me.
I'm using MVC to consume a Web Api service.

        // GET: Products/ProductsByCategory/5
        public async Task<ActionResult> ProductsByCategory(int id)
        {
            string url = string.Format("{0}api/categories/{1}/products", baseUrl, id);
            Service<GetProductsByCategoryID> service = new Core.Service<GetProductsByCategoryID>(url);
            return PartialView(await service.GetListAsync());
        }

        public async Task<List<T>> GetListAsync()
        {
            using (HttpClient client = new HttpClient())
            {
                client.BaseAddress = new Uri(url);

                return JsonConvert.DeserializeObject<List<T>>(
                    await client.GetStringAsync(url)
                );
            }
        }

Open in new window

0
Comment
Question by:JRockFL
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 41838592
Looks fine to me.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 41838603
P.S.

One thing to note--slightly off-topic--is that HttpClient is designed to be reused for multiple requests. So rather than wrapping your instance in a using you might consider widening the scope of that variable such that you can re-use the single instance across requests.
0
 
LVL 8

Author Comment

by:JRockFL
ID: 41838696
Thank you for your replies.
Also, thank you for the suggestion regarding HttpClient and being reused. Most of the examples I have seen use the using block.

But now I have located some topics regarding HttpClient and re usability.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question