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无法访问已释
- 使用 rss + c# 2022-01-01
- 是否可以在 .Net 3.5 中进行通用控件? 2022-01-01
- 为什么 C# 中的堆栈大小正好是 1 MB? 2022-01-01
- C# 通过连接字符串检索正确的 DbConnection 对象 2022-01-01
- CanBeNull和ReSharper-将其用于异步任务? 2022-01-01
- 带问号的 nvarchar 列结果 2022-01-01
- 在 C# 中异步处理项目队列 2022-01-01
- Windows 喜欢在 LINUX 中使用 MONO 进行服务开发? 2022-01-01
- Azure Active Directory 与 MVC,客户端和资源标识同一 2022-01-01
- 在 LINQ to SQL 中使用 contains() 2022-01-01