10

I am trying to go through a tutorial explaining how to access a WebAPI service in VS2013 (.net 4.5.1) and I get compilation errors with lines :

Product product = await response.Content.ReadAsAsync<Product>();
response = await client.PostAsJsonAsync("api/products", gizmo);

and

response = await client.PutAsJsonAsync(gizmoUrl, gizmo);

I've referenced System.Net.Http which apparently contains the three methods which fails to compile: ReadAsAsync(), PostAsJsonAsync() and PutAsJsonAsync(). Although the extensions class does not appear in the ObjectBrowser for the assembly so I'm not convinced I have the right version (version I have is 4.0.30319.18402).

I'm using the latest nuGet Microsoft.AspNet.WebApi.Client package (5.1.2) so I think I have everything required.

Can anyone see why the code doesn't compile or what I'm missing:

using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;

namespace WebApiClient
{
class Program
{
    static void Main()
    {
        RunAsync().Wait();
    }
    static async Task RunAsync()
    {
        using (var client = new HttpClient())
        {
            client.BaseAddress = new Uri("http://localhost:54122/");
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            // HTTP GET
            HttpResponseMessage response = await client.GetAsync("api/products/1");
            if (response.IsSuccessStatusCode)
            {
                //***********
                Product product = await response.Content.ReadAsAsync<Product>();
                //***********
                Console.WriteLine("{0}\t${1}\t{2}", product.Name, product.Price, product.Category);
            }

            // HTTP POST
            var gizmo = new Product() { Name = "Gizmo", Price = 100, Category = "Widget" };

            //***********
            response = await client.PostAsJsonAsync("api/products", gizmo);
            //***********
            if (response.IsSuccessStatusCode)
            {
                Uri gizmoUrl = response.Headers.Location;

                // HTTP PUT
                gizmo.Price = 80;   // Update price

                //***********
                response = await client.PutAsJsonAsync(gizmoUrl, gizmo);
                //***********

                // HTTP DELETE
                response = await client.DeleteAsync(gizmoUrl);
            }
        }
    }
}
}

Thanks.

Badgerspot
  • 2,301
  • 3
  • 28
  • 42
  • possible duplicate of [Where is HttpContent.ReadAsAsync?](http://stackoverflow.com/questions/10399324/where-is-httpcontent-readasasync) – Keith Pinson Jun 27 '14 at 18:52

1 Answers1

22

Turns out I needed a reference to System.Net.Http.Formatting.

I'd got the impression this was part of the nuGet Microsoft.AspNet.WebApi.Client package.

Badgerspot
  • 2,301
  • 3
  • 28
  • 42
  • Are you saying you installed the `Microsoft.AspNet.WebApi.Client` package but the reference wasn't added to your project? – Yishai Galatzer Apr 29 '14 at 14:27
  • Yes, that's right. It only appeared when I installed another NuGet package (WebAPIDoodle.Http), which I installed purely because I thought it would give me System.Net.Http.Formatting. – Badgerspot Apr 29 '14 at 14:34
  • I just tried it again in VS 2013 in a brand new console app, and it does work correctly. I'm guessing you might have installed the client package to another project accidentally. However if you can get a solid repro for installing the package and not getting references please open a bug for us on http://aspnetwebstack.codeplex.com/workitem/list/basic. Thanks! – Yishai Galatzer Apr 29 '14 at 14:38
  • Thanks Yishai. I tried it on a blank solution and it worked. Must have been user error. Thanks again. – Badgerspot Apr 30 '14 at 07:28