Search code examples
c#windowswebuwphttpclient

HttpClient.GetAsync works in one class but not in the other


This is where it works:

public class ThisWorks : IPortableHttpClient
{
    private Windows.Web.Http.HttpClient client;

    async public Task<Windows.Web.Http.HttpResponseMessage> GetAsync(Uri url)
    {
        return await client.GetAsync(url);
    }

And this does not work and I don't understand why:

public class DoesntWork : IPortableHttpClient
{
    private Windows.Web.Http.HttpClient client;

    async public Task<Windows.Web.Http.HttpResponseMessage> GetAsync(Uri url)
    {
        return await client.GetAsync(url);
    }

This gets an error message: await client.GetAsync(url) And the errormessage is this: 'IAsyncOperationWithProgress' does not contain a definition for 'GetAwaiter' and no extension method 'GetAwaiter' accepting a first argument of type 'IAsyncOperationWithProgress' could be found (are you missing a using directive for 'System')?

If it matters, here is IPortableHttpClient:

public interface IPortableHttpClient
{
    Task<HttpResponseMessage> GetAsync(Uri url);        
}

Another information that could be useful: The code where it works is in a UWP framework. It does not work in a .NET framework.


Solution

  • I've been using the wrong Namespace.

    I had to change Windows.Web.Http to System.Net.Http