简体   繁体   中英

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.

I've been using the wrong Namespace.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM