Search code examples
c#.netwindows-store-appsasync-awaitwindows-store

Task Error when await in windows store application


Hej, I have a method:

    public static async Task<myClassl> GetData()
    {
        HttpClient client = new HttpClient();
        client.BaseAddress = new Uri("http:sasa.com");
        HttpResponseMessage response = await client.GetAsync("api/GetData");
        myClassl data = await response.Content.ReadAsAsync<myClassl>();

        return data ;
    }

And when I write

myClassl  t = await DataGetter.GetData();

I have: The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task'.


Solution

  • You need to flag your method async where you write:

    // Add async to your calling method
    private async Task SomeOtherMethod()
    {
        myClassl t = await DataGetter.GetData();
    

    Any method that uses await internally must be an async method itself.