Solved

Async and await with MVC and Web Api

Posted on 2016-10-11
3
29 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 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
Looks fine to me.
0
 
LVL 74

Expert Comment

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
javascript call 6 35
Object Oriented Best Practice 5 32
C# code editing and collaboration 3 39
Check my code out(2) 1 26
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

744 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

12 Experts available now in Live!

Get 1:1 Help Now