Solved

Async and await with MVC and Web Api

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

738 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