Solved

Async and await with MVC and Web Api

Posted on 2016-10-11
3
38 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
  • 2
3 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to deploy to IIS 7 on 2008R2 server with DUO 2-step login? 2 36
ADO.NET ENTITY DATA MODEL 3 32
Host asp.net pages 5 25
System.Speech 2 15
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

895 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now