代码之家  ›  专栏  ›  技术社区  ›  Itamar Kerbel

从任务中自动删除等待时,SendAsync失败

  •  -1
  • Itamar Kerbel  · 技术社区  · 5 年前

     using (StringContent stringContent = new StringContent(payload, Encoding.UTF8, "application/json"))
     {
         HttpRequestMessage request = new HttpRequestMessage()
         //populate the request
         request.Content = stringContent;
         try
         {
            using (HttpResponseMessage response = await client
             .SendAsync(request, HttpCompletionOption.ResponseHeadersRead)
             .ConfigureAwait(false))
           {
              response.EnsureSuccessStatusCode();
              _lastRespose = await response.Content.ReadAsStringAsync();
              return _lastRespose;
           }
        }
        catch (Exception)
        { ... }
    

    这很有效。 我想让这段代码异步运行。 所以我把它改成了获取任务,并使用ContinueWith在通话结束后获得回电

     private async void OnTestLoginCallCompleate(Task<HttpResponseMessage> response)
     {
         if (response.IsFaulted)
         {
             ShowErrorMessage();
             return;
         }
     }
    
        private void Login(Action<Task<HttpResponseMessage>> loginCallback)
        {
            _username = userNameTB.Text;
            _password = pwTB.Text;
            _url = urlTB.Text;
    
            if (String.IsNullOrEmpty(_username) ||
                String.IsNullOrEmpty(_password) ||
                String.IsNullOrEmpty(_url))
            {
                MessageBox.Show("All fields must not be empty", "Validation Error", MessageBoxButtons.OK);
            }
    
            HttpRequestMessage request = new HttpRequestMessage()
            {
                RequestUri = new Uri(_url),
                Method = HttpMethod.Post,
            };
    
            request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            String payload = "{ \"email\": \"" + _username + "\", \"password\": \"" + _password + "\" }";
    
            using (StringContent stringContent = new StringContent(payload, Encoding.UTF8, "application/json"))
            {
                request.Content = stringContent;
                client.SendAsync(request, HttpCompletionOption.ResponseContentRead).ContinueWith(loginCallback);
            }
        }
    

    response.IsFaulted 是的。

    0 回复  |  直到 5 年前
        1
  •  1
  •   Itamar Kerbel    5 年前

    正如在注释中提到的,问题来自于我用 using . 这将处理HttpResponseMessage,当它到达回调时,它已经被处理了。 解决办法是移除 使用

    private async void OnTestLoginCallCompleate(Task<HttpResponseMessage> response)
     {
         if (response.IsFaulted)
         {
             ShowErrorMessage();
             return;
         }
         HttpResponseMessage result = response.Result;
        ......code....
         result.Dispose();
     }
    
    

    拆下使用部件并获得:

    StringContent stringContent = new StringContent(payload, Encoding.UTF8, "application/json");
    request.Content = stringContent;
    client.SendAsync(request, HttpCompletionOption.ResponseContentRead).ContinueWith(loginCallback);
    
        2
  •  -2
  •   Shahid    5 年前

    通过查看代码,特别是这一部分:

    using (StringContent stringContent = new StringContent(payload, Encoding.UTF8, "application/json"))
    {
       request.Content = stringContent;
       client.SendAsync(request, HttpCompletionOption.ResponseContentRead).ContinueWith(OnTestLoginCallCompleate);
    }
    

    您需要添加等待 client.SendAsync 以便继续正常工作。

    如果您尝试按以下方式修改代码,它应该可以工作。我已经在我的机器上进行了本地测试(稍加修改,所以运行正常)。

    using (StringContent stringContent = new StringContent(payload, Encoding.UTF8, "application/json"))
    {
       request.Content = stringContent;
       await client.SendAsync(request, HttpCompletionOption.ResponseContentRead).ContinueWith(OnTestLoginCallCompleate);
    }