简体   繁体   English

如何从异步中返回一个字符串

[英]How to return a string from async

My method is calling a web service and working asynchronusly. 我的方法是调用Web服务并异步工作。

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 但是当打印结果我看到的东西是这样的: System.Threading.Tasks.Task

how can I get the result string as I did with data inside my method. 如何获取结果字符串,就像我在方法中使用数据一样。

You need to do this since you are calling the async method synchronously : 您需要执行此操作,因为您同步调用async方法:

  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. Task<string>返回类型视为将来返回值的“promise”。

If you called the async method asynchronously then it would be like the following: 如果您异步调用异步方法,那么它将如下所示:

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

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: 为了获得包含在该任务中的实际值,您应该await它:

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. 这既自然又正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM