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无法访问已释
基础教程推荐
- 创建属性设置器委托 2022-01-01
- 如何使用OpenXML SDK将Excel转换为CSV? 2022-01-01
- 覆盖 Json.Net 中的默认原始类型处理 2022-01-01
- Page.OnAppearing 中的 Xamarin.Forms Page.DisplayAlert 2022-01-01
- 我什么时候应该使用 GC.SuppressFinalize()? 2022-01-01
- C# - 如何列出发布到 ASPX 页面的变量名称和值 2022-01-01
- 使用 SED 在 XML 标签之间提取值 2022-01-01
- 当键值未知时反序列化 JSON 2022-01-01
- C# - 将浮点数转换为整数...并根据余数更改整数 2022-01-01
- 从 VB6 迁移到 .NET/.NET Core 的最佳策略或工具 2022-01-01