Azure函数http客户端对象DisposedException无法访问已释

Azure function httpclient ObjectDisposedException Cannot access a disposed object SslStream(Azure函数http客户端对象DisposedException无法访问已释放的对象SslStream)

本文介绍了Azure函数http客户端对象DisposedException无法访问已释放的对象SslStream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我从Azure函数发布请求时,我收到此ObjectDisposedException。我在真实的Azure函数环境和函数本地调试中都看到了这个问题。我认为这是由于目标服务的大型响应机构造成的。但不确定。 下面是代码和详细的错误消息。我在"Await 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.
Object name: 'SslStream'.
   at System.Net.Security.SslState.ThrowIfExceptional()

   at System.Net.Security.SslState.CheckThrow(Boolean authSuccessCheck, Boolean shutdownCheck)
  
   at System.Net.Security.SslState.CheckOldKeyDecryptedData(Memory`1 buffer)
 
   at System.Net.Security.SslState.HandleQueuedCallback(Object& queuedStateRequest)
--- End of stack trace from previous location where exception was thrown ---
  
   at System.Net.Security.SslStreamInternal.ReadAsyncInternal[TReadAdapter](TReadAdapter adapter, Memory`1 buffer)
   --- End of inner exception stack trace ---
  
   at System.Net.Security.SslStreamInternal.ReadAsyncInternal[TReadAdapter](TReadAdapter adapter, Memory`1 buffer)
  
   at System.Net.Http.HttpConnection.FillAsync()
   at System.Net.Http.HttpConnection.ReadNextResponseHeaderLineAsync(Boolean foldedHeadersAllowed)
   
   at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)
   --- End of inner exception stack trace ---
 
   at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)
   
   at System.Net.Http.HttpConnectionPool.SendWithNtConnectionAuthAsync(HttpConnection connection, HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
 
   at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
  
   at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
 
   at System.Net.Http.DiagnosticsHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
  
   at System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
  
   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"```

推荐答案

请检查using statement 的工作方式。

using块结束时,您的代码中的客户端将是disposed,但您在content上仍有挂起的任务将尝试访问它。您需要在客户端使用块时获取结果

这篇关于Azure函数http客户端对象DisposedException无法访问已释放的对象SslStream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Azure函数http客户端对象DisposedException无法访问已释

基础教程推荐