.NET Tips and Tricks

Blog archive

Pulling Objects from Web Services with ReadAsAsync

In an earlier post, I discussed the three objects that Microsoft has provided for calling Web Services: HttpWebRequest, WebClient and HttpClient. At the time, I suggested WebClient was the simplest solution, unless you wanted to take advantage of HttpClient's asynchronous processing.

I've reconsidered that choice since then and I'm currently using HttpClient almost exclusively. Part of the reason is that HttpClient gives me the ReadAsAsync method. To understand why I like that method so much, you have to compare it to the alternatives.

Here's how to get a list of Customer objects out of the response from a Web Service using the traditional ReadAsStringAsync method:

HttpClient hc = new HttpClient();
HttpResponseMessage customersRm = await hc.GetAsync(url);
string customersString = await customersRm.Content.ReadAsStringAsync();
List<Customer> custs = JsonConvert.DeserializeObject<List<Customer>>(customersString);

Now here's the code using ReadAsAsync (even the method name is shorter!):

HttpClient hc = new HttpClient();
HttpResponseMessage customersRm = await hc.GetAsync(url);
List<Customer> custs = await customersRm.Content.ReadAsAsync<List<Customer>>();

The only problem is that you don't, in .NET 4.5 or later, get ReadAsAsync without some work -- you'll have to add the Microsoft.AspNet.WebApi.Client NuGet package to your project to pick up this extension method. I think that's worth doing.

Posted by Peter Vogel on 04/25/2019


comments powered by Disqus

Featured

  • Compare New GitHub Copilot Free Plan for Visual Studio/VS Code to Paid Plans

    The free plan restricts the number of completions, chat requests and access to AI models, being suitable for occasional users and small projects.

  • Diving Deep into .NET MAUI

    Ever since someone figured out that fiddling bits results in source code, developers have sought one codebase for all types of apps on all platforms, with Microsoft's latest attempt to further that effort being .NET MAUI.

  • Copilot AI Boosts Abound in New VS Code v1.96

    Microsoft improved on its new "Copilot Edit" functionality in the latest release of Visual Studio Code, v1.96, its open-source based code editor that has become the most popular in the world according to many surveys.

  • AdaBoost Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the AdaBoost.R2 algorithm for regression problems (where the goal is to predict a single numeric value). The implementation follows the original source research paper closely, so you can use it as a guide for customization for specific scenarios.

  • Versioning and Documenting ASP.NET Core Services

    Building an API with ASP.NET Core is only half the job. If your API is going to live more than one release cycle, you're going to need to version it. If you have other people building clients for it, you're going to need to document it.

Subscribe on YouTube