#c# #asp.net #.net #async-await #httpresponse
Вопрос:
Я получаю исключение ниже в случае 1, но случаи 2 и 3 работают нормально. Является ли случай 1 попаданием в тупиковую ситуацию в ожидании ответа.
public class MessageHandler1 : DelegatingHandler
{
protected async override Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request, CancellationToken cancellationToken)
{
Debug.WriteLine("Process request");
var response = await this.ProcessRequestAsync(request, cancellationToken);
Debug.WriteLine("Process response");
return response;
}
}
случай 1:
private async Task<HttpResponseMessage> ProcessRequestAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
using(HttpClientHandler clienthandler = new HttpClientHandler())
{
using(var client = new HttpClient(clienthandler))
{
using(var clonedRequest = await HttpRequestMessageExtensions.CloneHttpRequestMessageAsync(request))
{
...
var response = await client.SendAsync(clonedRequest, HttpCompletionOption.ResponseContentRead,
cancellationToken);
...
return response;
}
}
}
}
случай 2: Добавлена настройка(ложь) в SendAsync
private async Task<HttpResponseMessage> ProcessRequestAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
using(HttpClientHandler clienthandler = new HttpClientHandler())
{
using(var client = new HttpClient(clienthandler))
{
using(var clonedRequest = await HttpRequestMessageExtensions.CloneHttpRequestMessageAsync(request))
{
...
var response = await client.SendAsync(clonedRequest, HttpCompletionOption.ResponseContentRead,
cancellationToken).ConfigureAwait(false);
...
return response;
}
}
}
}
случай 3: Удалена упаковка с использованием для httpclient
private async Task<HttpResponseMessage> ProcessRequestAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
using(HttpClientHandler clienthandler = new HttpClientHandler())
{
var client = new HttpClient(clienthandler);
var clonedRequest = await HttpRequestMessageExtensions.CloneHttpRequestMessageAsync(request);
...
var response = await client.SendAsync(clonedRequest, HttpCompletionOption.ResponseContentRead, cancellationToken);
...
return response;
}
}
Exception: Error while copying content to a stream.
InnerException":{
"ClassName":"System.Net.WebException",
"Message":"The request was aborted: The request was canceled.",
"Data":null,
"InnerException":null,
"HelpURL":null,"StackTraceString":" at System.Net.ConnectStream.BeginRead(Byte[] buffer, Int32 offset, Int32 size, AsyncCallback callback, Object state)
at System.Net.Http.HttpClientHandler.WebExceptionWrapperStream.BeginRead(Byte[] buffer, Int32 offset, Int32 count, AsyncCallback callback, Object state)",
"RemoteStackTraceString":null,
"RemoteStackIndex":0,
"ExceptionMethod":"8
BeginRead
System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
System.Net.ConnectStream
System.IAsyncResult BeginRead(Byte[], Int32, Int32, System.AsyncCallback, System.Object)",
"HResult":-2146233079,
"Source":"System",
"WatsonBuckets":null
}
'Microsoft.Owin.Host.HttpListener.RequestProcessing.HttpListenerStreamWrapper'.",
"Data":null,
"InnerException":{
"ClassName":"System.NotSupportedException",
"Message":"This stream does not support seek operations.",
"Data":null,
"InnerException":null,
"HelpURL":null,
"StackTraceString":" at System.Net.HttpResponseStream.get_Length()
at GetLength(Object )
at Newtonsoft.Json.Serialization.DynamicValueProvider.GetValue(Object target)",
"RemoteStackTraceString":null,
"RemoteStackIndex":0,
"ExceptionMethod":"8
get_Length
System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
System.Net.HttpResponseStream
Int64 get_Length()",
"HResult":-2146233067,
"Source":"System",
"WatsonBuckets":null
}