Azure函数httpclient ObjectDisposedException无法访问已释放的对象SslStream



当我从Azure函数执行Post请求时,我得到了此ObjectDisposedException。我在真实的azure函数环境和函数本地调试中都看到了这个问题。我相信这是由于目标服务的大量响应。但不确定。

以下是代码和详细的错误消息。我在"等待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.rnObject name: 'SslStream'.rn   at System.Net.Security.SslState.ThrowIfExceptional()rn
at System.Net.Security.SslState.CheckThrow(Boolean authSuccessCheck, Boolean shutdownCheck)rn  
at System.Net.Security.SslState.CheckOldKeyDecryptedData(Memory`1 buffer)rn 
at System.Net.Security.SslState.HandleQueuedCallback(Object& queuedStateRequest)rn--- End of stack trace from previous location where exception was thrown ---rn  
at System.Net.Security.SslStreamInternal.ReadAsyncInternal[TReadAdapter](TReadAdapter adapter, Memory`1 buffer)rn   --- End of inner exception stack trace ---rn  
at System.Net.Security.SslStreamInternal.ReadAsyncInternal[TReadAdapter](TReadAdapter adapter, Memory`1 buffer)rn  
at System.Net.Http.HttpConnection.FillAsync()rn   at System.Net.Http.HttpConnection.ReadNextResponseHeaderLineAsync(Boolean foldedHeadersAllowed)rn   
at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)rn   --- End of inner exception stack trace ---rn 
at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)rn   
at System.Net.Http.HttpConnectionPool.SendWithNtConnectionAuthAsync(HttpConnection connection, HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)rn 
at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)rn  
at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)rn 
at System.Net.Http.DiagnosticsHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)rn  
at System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)rn  
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块结束,客户端将为disposed,但content上仍有挂起的任务将尝试访问它。在客户端中使用块时,需要获取结果。

最新更新