user1644160
user1644160

Reputation: 277

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'.

Upvotes: 0

Views: 387

Answers (1)

Reed Copsey
Reed Copsey

Reputation: 564323

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.

Upvotes: 3

Related Questions