Click here to Skip to main content
15,915,611 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I have the following codes in which I will get datetime from a WebAPI that is hosted on a server.
But when I try out the codes, it returns me the error message:
task string does not contain a definition for getawaiter and no extension.......

C#
public async void RegenerateServerDate()
{
    DateTime dateTime = new DateTime();
    
    using (var client = new HttpClient())
    {
        client.BaseAddress = new Uri("https://localhost:8080");
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        //error message here
        HttpResponseMessage response = await client.GetAsync("api/servertime");
        if (response.IsSuccessStatusCode)
        {
            //error message here
            string result = await response.Content.ReadAsStringAsync();
            result = Regex.Replace(result, @"[A-Za-z\]""", "");
            dateTime = DateTime.Parse(result);
        }
    }

}


What I have tried:

1. Tired by creating another WindowForm and apply the above codes into a button click event. Runs well without error.
Posted
Updated 1-May-16 23:41pm
Comments
Richard Deeming 15-Apr-16 14:39pm    
What version of .NET are you targeting in the project that doesn't work? It needs to be 4.5 or higher.
Jamie888 17-Apr-16 23:11pm    
Yes sir, I have found it after I posted the question. I have changes it to .NET 4.5.1 and the error message is gone now. But another problem occurs in which when I tried to debug/run the solution, it has some runtime error in which the cs file I used to put the codes above is in .NET 4.5.1 whereas the other cs files are in .NET 4. Anyway to solve it without changing the .NET framework for other cs files?
Richard Deeming 18-Apr-16 8:08am    
Solve what? You haven't told us what the problem is.

Remember, we can't see your screen, access your computer, or read your mind. If you don't tell us what the problem is, we can't help you.

1 solution

There are multiple possibilities why you are seeing this error message, please take a look at this :

task-does-not-contain-a-definition-for-getawaiter


Hope it helps.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900