Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return a string from async

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.

like image 332
eeadev Avatar asked Jul 21 '15 10:07

eeadev


People also ask

Can an async function return a value?

Async functions always return a promise. If the return value of an async function is not explicitly a promise, it will be implicitly wrapped in a promise. Note: Even though the return value of an async function behaves as if it's wrapped in a Promise.resolve , they are not equivalent.

What is return type of async?

Async methods have three possible return types: Task<TResult>, Task, and void. In Visual Basic, the void return type is written as a Sub procedure. For more information about async methods, see Asynchronous Programming with Async and Await (Visual Basic).

Can async return void?

Event handlers naturally return void, so async methods return void so that you can have an asynchronous event handler.


1 Answers

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);
like image 167
Jamie Rees Avatar answered Sep 23 '22 12:09

Jamie Rees