【问题标题】:Azure function httpclient ObjectDisposedException Cannot access a disposed object SslStreamAzure 函数 httpclient ObjectDisposedException 无法访问已处置的对象 SslStream
【发布时间】:2020-06-24 22:32:00
【问题描述】:

当我从 Azure 函数发出 Post 请求时,我得到了这个 ObjectDisposedException。我在真正的天蓝色函数环境和函数本地调试中都看到了这个问题。我相信这是由于目标服务的大量响应体而发生的。但不确定。

下面是代码和详细的错误信息。我在“等待 httpClient.SendAsync(requestMessage).ConfigureAwait(false)”行中收到此错误

此代码运行良好,并在不在 Azure func 环境中的本地脚本中尝试时得到 200 响应。

 try
            {
                using (HttpResponseMessage response = await httpClient.SendAsync(requestMessage).ConfigureAwait(false))
                {
                    var responseHeaders = string.Join(" | ", response.Headers.Select(h => $"{h.Key} : {h.Value}"));
                    sbHeaders.Append($" :: Response- {responseHeaders}");

                    string content = await response.Content.ReadAsStringAsync();
                    try
                    {
                        response.EnsureSuccessStatusCode();
                    }
                    catch (HttpRequestException ex)
                    {
                        // This try catch is to handle any unsuccessful service response (service has handled the request)
                        string errorMessage = $"{requestMessage.RequestUri.ToString()} failed!. Headers: {sbHeaders.ToString()} :: Server response: {content}";
                        throw new CustomException(serviceAlias, response.StatusCode, errorMessage, ex);
                    }

                    var responseEntity = JsonConvert.DeserializeObject<TResponse>(content);
                    return responseEntity;
                }
            }
            catch (Exception ex)
            {
                // This try catch is to handle any network error (service HAS NOT handled the request)
                string errorMessage = $"{requestMessage.RequestUri.ToString()} failed!. Headers: {sbHeaders.ToString()} :: Server response: [{ex.GetType().Name}]{ex.Message}";
                throw new CustomException(serviceAlias, HttpStatusCode.InternalServerError, errorMessage, ex);
            }



System.IO.IOException: The read operation failed, see inner exception. ---> 
System.ObjectDisposedException: Cannot access a disposed object.\r\nObject name: 'SslStream'.\r\n   at System.Net.Security.SslState.ThrowIfExceptional()\r\n
   at System.Net.Security.SslState.CheckThrow(Boolean authSuccessCheck, Boolean shutdownCheck)\r\n  
   at System.Net.Security.SslState.CheckOldKeyDecryptedData(Memory`1 buffer)\r\n 
   at System.Net.Security.SslState.HandleQueuedCallback(Object& queuedStateRequest)\r\n--- End of stack trace from previous location where exception was thrown ---\r\n  
   at System.Net.Security.SslStreamInternal.ReadAsyncInternal[TReadAdapter](TReadAdapter adapter, Memory`1 buffer)\r\n   --- End of inner exception stack trace ---\r\n  
   at System.Net.Security.SslStreamInternal.ReadAsyncInternal[TReadAdapter](TReadAdapter adapter, Memory`1 buffer)\r\n  
   at System.Net.Http.HttpConnection.FillAsync()\r\n   at System.Net.Http.HttpConnection.ReadNextResponseHeaderLineAsync(Boolean foldedHeadersAllowed)\r\n   
   at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)\r\n   --- End of inner exception stack trace ---\r\n 
   at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)\r\n   
   at System.Net.Http.HttpConnectionPool.SendWithNtConnectionAuthAsync(HttpConnection connection, HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)\r\n 
   at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)\r\n  
   at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)\r\n 
   at System.Net.Http.DiagnosticsHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)\r\n  
   at System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)\r\n  
   at Microsoft.Ingestion.Stitch.Function.StitchServiceClient.SendRequestMessageInternalAsync[TResponse](HttpRequestMessage requestMessage, MicroServiceAlias serviceAlias) in E:\\Agent_work\\21\\s\\Src\\Stitch.Function\\Clients\\StitchServiceClient.cs:line 229"```

【问题讨论】:

  • 不,我尝试过使用和不使用块。它没有帮助同样的错误。

标签: c# .net-core azure-functions dotnet-httpclient


【解决方案1】:

请检查using statement 的工作原理。

一旦using 块结束,您的代码客户端将是disposed,但您在content 上仍有待处理的任务将尝试访问它。您需要在客户端中使用块获得结果

【讨论】:

  • 我试过不使用语句。它没有帮助:(无论使用和不使用语句,我都会遇到同样的错误。
  • 你的sdk版本是什么? fix 将与下一个 2.2.x 版本一起发布,即 2.2.7。那将是下个月的某个时候。它还将与下一个 2.1.x 版本一起发布,即 2.1.13。这应该与 2.2.x 的发布时间相同。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-20
  • 1970-01-01
  • 2011-10-23
  • 2011-03-25
相关资源
最近更新 更多