Compilation error on ReadAs() method when using WCF Web API tutorial

Topics: Web Api
Jan 10, 2012 at 9:11 PM

I am using the help file WCFWebAPI_Con.chm to follow the section "Write a Web API Client in C#" of the tutorial "Getting Started with WCF Web API". When I compile the project, I get the following error on the ListAllContacts() method shown below:

Error:

<<System.Net.Http.HttpContent' does not contain a definition for 'ReadAs' and no extension method 'ReadAs' accepting a first argument of type 'System.Net.Http.HttpContent' could be found (are you missing a using directive or an assembly reference?)>>

Method:

using System.Net.Http;

using System.Web;
using ContactManager;
using ContactManager.Resources;

static void ListAllContacts()
        {
            HttpClient client = new HttpClient();

            HttpResponseMessage resp = client.GetAsync("http://localhost:9000/api/contacts").Result;
            if (resp.IsSuccessStatusCode)
            {
                List contacts = resp.Content.ReadAs>();
                //System.Threading.Tasks.Task> contacts = resp.Content.ReadAsAsync>();

                foreach (Contact c in contacts)
                {
                    Console.WriteLine("{0}: {1}", c.ContactId, c.Name);
                }
            }
        }
Please help. Thanks.
Saf
Coordinator
Jan 10, 2012 at 10:24 PM

Sorry, the docs in the .chm are a bit stale. In the latest Preview the HttpClient only supports async operations. So try ReadAsAsync<T>(…).

Daniel Roth

Jan 11, 2012 at 4:00 PM

Dan, I had tried ReadAsAsync<T>(...) as you may have noticed the line commented out below the ReadAs operation in my original post. But ReadAS Async<T>(...) returrns an object that does not support the foreach loop. How can I loop through the contacts?

I could not find any documentation for httpContent class for .Net 4.0 (the framework that the tutorial is using) and this MSDN link for the .NET 4.5 does not indicate any such method (ReadAsAsync<T>(...) for this class.

Thanks,

Saf

Jan 11, 2012 at 4:13 PM

The easiest thing to do is add ".Result" just like you did on your GetAsync call.