Azure function httpclient ObjectDisposedException Cannot access a disposed object 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无法访问已释
				
        
 
            
        基础教程推荐
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
 - 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
 - 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
 - JSON.NET 中基于属性的类型解析 2022-01-01
 - 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
 - 首先创建代码,多对多,关联表中的附加字段 2022-01-01
 - 全局 ASAX - 获取服务器名称 2022-01-01
 - 错误“此流不支持搜索操作"在 C# 中 2022-01-01
 - 如何动态获取文本框中datagridview列的总和 2022-01-01
 - 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
 
    	
    	
    	
    	
    	
    	
    	
    	
						
						
						
						
						
				
				
				
				