Click here to Skip to main content
15,908,020 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
Hi, I have the following codes in .NET 4.0 using VS2010. I have researched online in which .NET 4.0 does not support the codes as below directly. One way is to use Task object which has the same output that I desired. Below are my codes in which I have written in .NET 4.5.1 but I will have to downgrade into .NET 4.0 for some purpose

C#
public async void RegenerateServerDate()
        {
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("https://localhost:8089");
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                
                
                HttpResponseMessage response = await client.GetAsync("api/servertime");
                if (response.IsSuccessStatusCode)
                {
                    string result = await response.Content.ReadAsStringAsync();
                    result = Regex.Replace(result, @"[A-Za-z]", " ").Replace("\"", "");
                }
            }     
        }



How can I convert the above codes within Task object? Any help would be appreciated.

What I have tried:

1. Add Task in front of await keyword but hit error.
Posted
Updated 18-Apr-16 22:35pm
v2

C#
using (var client = new HttpClient())
{
    client.BaseAddress = new Uri("https://localhost:8089");
    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));


    HttpResponseMessage response = client.GetAsync("api/servertime").Result;
    if (response.IsSuccessStatusCode)
    {
        string result = response.Content.ReadAsStringAsync().Result;
        result = Regex.Replace(result, @"[A-Za-z]", " ").Replace("\"", "");
        dateTime = DateTime.Parse(result);
    }
    else
    {
        //dateTime = DateHelper.ConvertToUtc(DateTime.Now);
        dateTime = DateTime.UtcNow;
    }
}
 
Share this answer
 
Hello Jamie,

Since you are using VS 2010, resources say, VS 2010 does not support Async. Please follow the below link.

c# - in .net 4.0 I have not avaliable the async/await keywords -[^]

Check here: They suggest using Async CTP if you are on VS 2010
.net - Will VS 2010 allow me to use the new async and await keywords in C#?[^]

c# - Proper way to use Async with VS 2010 now that VS 2012 is released?[^]
Thanks
 
Share this answer
 
v2
Comments
Jamie888 18-Apr-16 7:04am    
So sir, does it means I have no choice but to use a newer version of .NET framework in order to use async programming?
Passion4Code 18-Apr-16 7:47am    
Please check the updated answer. They are asking to add Async CTP (installation)
Jamie888 19-Apr-16 4:34am    
Thank you sir. After some thought, I have decided to part way with the async programming. I will write my answer in below.
R. Erasmus 19-Apr-16 5:14am    
Good answer!
Passion4Code 19-Apr-16 6:22am    
Thanks Sir! :)
 
Share this answer
 
Comments
Jamie888 18-Apr-16 6:23am    
Hi sir, I have tried to modify my codes as below:
Task<httpresponsemessage> response = client.GetAsync("api/servertime"));
if (response.Result.IsSuccessStatusCode)
{
//error here
string result = await response.Result.Content.ReadAsStringAsync();

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