28

My method is calling a web service and working asynchronusly.

When getting response, everything works fine and I am getting my response.

The problem starts when I need to return this response.

here is the code of my method:

 public async Task<string> sendWithHttpClient(string requestUrl, string json)
        {
            try
            {
                Uri requestUri = new Uri(requestUrl);
                using (var client = new HttpClient())
                {
                    client.DefaultRequestHeaders.Clear();
                    ...//adding things to header and creating requestcontent
                    var response = await client.PostAsync(requestUri, requestContent);

                    if (response.IsSuccessStatusCode)
                    {

                        Debug.WriteLine("Success");
                        HttpContent stream = response.Content;
                        //Task<string> data = stream.ReadAsStringAsync();    
                        var data = await stream.ReadAsStringAsync();
                        Debug.WriteLine("data len: " + data.Length);
                        Debug.WriteLine("data: " + data);
                        return data;                       
                    }
                    else
                    {
                        Debug.WriteLine("Unsuccessful!");
                        Debug.WriteLine("response.StatusCode: " + response.StatusCode);
                        Debug.WriteLine("response.ReasonPhrase: " + response.ReasonPhrase);
                        HttpContent stream = response.Content;    
                        var data = await stream.ReadAsStringAsync();
                        return data;
                     }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("ex: " + ex.Message);
                return null;
            }

and I am calling it this way:

      Task <string> result =  wsUtils.sendWithHttpClient(fullReq, "");           
      Debug.WriteLine("result:: " + result); 

but when printing result I am seeing something like this: System.Threading.Tasks.Task

how can I get the result string as I did with data inside my method.

1
  • 1
    you need to access the Result property of your Task to get the desired output . Commented Jul 21, 2015 at 10:19

2 Answers 2

34

You need to do this since you are calling the async method synchronously:

  Task<string> result =  wsUtils.sendWithHttpClient(fullReq, "");           
  Debug.WriteLine("result:: " + result.Result); // Call the Result

Think of the Task<string> return type as a 'promise' to return a value in the future.

If you called the async method asynchronously then it would be like the following:

  string result =  await wsUtils.sendWithHttpClient(fullReq, "");           
  Debug.WriteLine("result:: " + result);
Sign up to request clarification or add additional context in comments.

4 Comments

I called it async, and it works, thanks. I will accept it soon.
The wait is not necessary. The Result property blocks the calling thread until the task finishes. See msdn.microsoft.com/en-us/library/dd537613(v=vs.110).aspx
I used it succesfully.
As soon as I read the line "Think of the Task<string> return type as a 'promise' to return a value in the future." I instantly realised my mistake. The await` was missing. I'm going to mentally bookmark this statement. Thank you!
10

An asynchronous method returns a task, representing a future value. In order to get the actual value wrapped in that task, you should await it:

string result = await wsUtils.sendWithHttpClient(fullReq, "");
Debug.WriteLine("result:: " + result);

Note that this will require your calling method to be asynchronous. This is both natural and correct.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.