Hello,
I use httpClient to call Web service, when i run project in Windows, i don't get any exception, but in Linux, it return

here is my code
```c#
//string Date = "date:" + UpperCaseUrlEncode(str);
string Date = "date: " + utcFormattedDate;
//Date = "date:Thu%2C+21+Apr+2016+13%3A32%3A18+UTC";
using (HMACSHA1 hmac = new HMACSHA1(_secretKeyUTF8))
{
byte[] Datebytes = Encoding.UTF8.GetBytes(Date);
byte[] hashedValue = hmac.ComputeHash(Datebytes);
string Signature = Convert.ToBase64String(hashedValue);
string Url_Encoded_Signature = UpperCaseUrlEncode(Signature);
request.Content = new StringContent(string.Empty, Encoding.UTF8, "application/json");
request.Headers.Add("Authorization", string.Format("Signature keyId=\"{0}\",algorithm=\"{1}\",signature=\"{2}\"", _applicationId, "hmac-sha1", Url_Encoded_Signature));
var field = typeof(HttpRequestHeaders).GetField("invalidHeaders",BindingFlags.NonPublic | BindingFlags.Static)?? typeof(HttpRequestHeaders).GetField("s_invalidHeaders", BindingFlags.NonPublic | BindingFlags.Static);
if (field != null)
{
var invalidFields = (HashSet<string>)field.GetValue(null);
invalidFields.Remove("Content-Type");
}
request.Content.Headers.Remove("Content-Type");
//request.Headers.Add("Accept-Encoding", "gzip");
request.Content.Headers.Add("Content-Type", "application/json;charset=UTF-8");
request.Content.Headers.TryAddWithoutValidation("Date", utcFormattedDate);
//request.SetTimeout(TimeSpan.FromSeconds(5));
}
response = await base.SendAsync(request, cancellationToken);
return response;
}
}
}
```
That means it timed out while trying to connect; the default timeout is 100 seconds. Most likely it's some kind of connectivity issue from your Linux machine you don't have with your Windows machine, different firewall settings, different DNS settings, different IP settings, different proxies, etc. From what you shared, there's no indication it's an issue in the libraries.
@stephentoub I have the exact same issue. I can access an API endpoint serving JSON data on my windows machine fine. On Linux it timesout and throws the following.
```C#
System.Threading.Tasks.TaskCanceledException
The operation was canceled.
System.Threading.Tasks.TaskCanceledException: The operation was canceled. ---> System.IO.IOException: Unable to read data from the transport connection: Operation canceled. ---> System.Net.Sockets.SocketException: Operation canceled
--- End of inner exception stack trace ---
at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error)
at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.GetResult(Int16 token)
at System.Net.Security.SslStreamInternal.
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 Microsoft.Extensions.Http.Logging.LoggingHttpMessageHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at CAFlow.Core.Filters.HttpLoggingHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) in /app/src/CAFlow.Core/Filters/HttpLoggingHandler.cs:line 29
at Microsoft.Extensions.Http.Logging.LoggingScopeHttpMessageHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
at CAFlow.Common.Extensions.HttpClientExtensions.SendRequestAsync(HttpClient http, Uri uri, Object body, String user, String pass) in /app/src/CAFlow.Common/Extensions/HttpClientExtensions.cs:line 75
at CAFlow.Common.Extensions.HttpClientExtensions.PostAsyncT in /app/src/CAFlow.Common/Extensions/HttpClientExtensions.cs:line 44
at CAFlow.Core.Services.CodeFixApiService.GetIssuesAsync(String repoUrl, String commit, String branch) in /app/src/CAFlow.Core/Services/CodeFixApiService.cs:line 58
at CAFlow.Core.Services.CodeFixImportService.ImportIssuesAsync(Guid caFlowBranchId) in /app/src/CAFlow.Core/Services/CodeFixImportService.cs:line 64
at CAFlow.Core.Services.Pipeline.CodeFixImportExecutor.ExecuteAsync(PipelineContext context) in /app/src/CAFlow.Core/Services/Pipeline/CodeFixImportExecutor.cs:line 29
at CAFlow.Core.Models.PipelineContext.DoAsync(PipelineJobOperation operation) in /app/src/CAFlow.Core/Models/PipelineContext.cs:line 115
at CAFlow.Core.Models.PipelineContext.DoAsync(PipelineJobOperation operation) in /app/src/CAFlow.Core/Models/PipelineContext.cs:line 143
at CAFlow.Core.Services.PipelineService.ExecuteRepoAnalysisAsync(PipelineContext ctx) in /app/src/CAFlow.Core/Services/PipelineService.cs:line 187
at CAFlow.Core.Services.PipelineService.DoRepoAnalysisAsync(PipelineContext ctx) in /app/src/CAFlow.Core/Services/PipelineService.cs:line 139
at CAFlow.Core.Services.PipelineService.ExecuteAsync(CAPipelineInput input, Guid pipelineJobId, PerformContext performContext) in /app/src/CAFlow.Core/Services/PipelineService.cs:line 131
at CAFlow.Core.Services.PipelineService.ExecuteAsync(Guid pipelineJobId, PipelineQueue queue, PerformContext performContext) in /app/src/CAFlow.Core/Services/PipelineService.cs:line 57
at CAFlow.Core.Services.PipelineService.ExecuteAsync(Guid pipelineJobId, PipelineQueue queue, PerformContext performContext) in /app/src/CAFlow.Core/Services/PipelineService.cs:line 62
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
upon digging, it turns out the problem is huge request data. The request data i am trying to access is around 25 MB JSON, (16MB minified JSON)
The requests in Linux machine using CURL work fine. However .NET HttpClient cannot handle them
The timeout was increased to 30 minutes. The average request response is around 7 minutes. But the request never completes
No Issue on Windows Machine.
Reproducible on Linux Machine.
Http Method: `POST`
the same endpoint works fine when it offers less data. increasing the data size causes the problem.
Here is the code which invokes the httpclient
```c#
private static async Task<HttpResponseMessage> SendRequestAsync(
HttpClient http,
Uri uri,
object body,
string user,
string pass)
{
if (user != null)
{
var byteArray = Encoding.UTF8.GetBytes($"{user}:{pass}");
http.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
}
var requestMessage = new HttpRequestMessage
{
Method = HttpMethod.Post,
RequestUri = uri
};
if (body != null)
{
requestMessage.Content = new StringContent(JsonConvert.SerializeObject(body, SerializerSettings), Encoding.UTF8, "application/json");
requestMessage.Content.Headers.ContentType.CharSet = null;
}
return await http.SendAsync(requestMessage);
}
.NET Core SDK : 2.2.103
Link to the Response JSON that Endpoint produces. https://filebin.net/6z9ptg7r53jjud0b/responseJson.txt?t=vuwt2asv (The link will expire in 1 week)
Endpoint response time: 409453 ms
Please let me know what else do u need to re-produce this
I have the exact same issue.
Thanks for the details, but it's not the same issue. It's the same top-level exception type, but there are multiple things that can lead to cancellation. The original poster's issue was a timeout during connect, whereas yours is a timeout during processing.
.NET Core SDK : 2.2.103
.NET Core 2.2 is end-of-life. @asadcr, can you try .NET Core 3.1?
The requests in Linux machine using CURL work fine.
So on the exact same machine, the request with CURL works ok but the request with HttpClient does not? How long does the CURL request take with the same inputs that are problematic with HttpClient? Does the issue go away in your .NET Core 2.2 code if you add AppContext.SetSwitch("System.Net.Http.UseSocketsHttpHandler", false); at the very beginning of your program?
@stephentoub , i use.NET Core 3.1, i have tried my project in many VM and i had same issue
Best regards
Hello @stephentoub ,
I have added AppContext.SetSwitch("System.Net.Http.UseSocketsHttpHandler", false);
I get error like message below

PS: The requests in Linux machine using CURL work fine
Best regards
@HASSEN-MEDDEB-ATOS, setting that switch changed the internal implementation to a completely different one based on curl, and it's failing as well.
The requests in Linux machine using CURL work fine
As I noted earlier, you'll need to debug why the connection isn't succeeding; this is not something we can do, it's specific to your environment. Maybe when you use CURL it's using a different proxy? Maybe it has different timeouts? Maybe it's using a different DNS resolver? etc. Your best bet is to use a tool like wireshark to look at the traffic and understand what's failing and why.
Does the issue go away in your .NET Core 2.2 code if you add AppContext.SetSwitch("System.Net.Http.UseSocketsHttpHandler", false); at the very beginning of your program?
@stephentoub Yes the issue goes away using that switch. The httpClient takes the same processing time as Curl on Linux and works fine now.
.NET Core 2.2 is end-of-life. @asadcr, can you try .NET Core 3.1?
Yes, i will setup the same scenario on .NET Core 3.1 shortly and will try if .NET Core has similar buggy behavior on Linux.
@stephentoub .NET Core 3.1.2 gives the similar exception
Please note that i have not added the switch yet
AppContext.SetSwitch("System.Net.Http.UseSocketsHttpHandler", false);
```c#
System.Threading.Tasks.TaskCanceledException
The operation was canceled.
System.Threading.Tasks.TaskCanceledException: The operation was canceled.
---> System.IO.IOException: Unable to read data from the transport connection: Operation canceled.
---> System.Net.Sockets.SocketException (125): Operation canceled
--- End of inner exception stack trace ---
at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error, CancellationToken cancellationToken)
at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.GetResult(Int16 token)
at System.Net.Security.SslStream.
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 Microsoft.Extensions.Http.Logging.LoggingHttpMessageHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at CAFlow.Core.Filters.HttpLoggingHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) in /app/src/CAFlow.Core/Filters/HttpLoggingHandler.cs:line 43
at Microsoft.Extensions.Http.Logging.LoggingScopeHttpMessageHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
at CAFlow.Common.Extensions.HttpClientExtensions.SendRequestAsync(HttpClient http, Uri uri, Object body, String user, String pass) in /app/src/CAFlow.Common/Extensions/HttpClientExtensions.cs:line 76
at CAFlow.Common.Extensions.HttpClientExtensions.PostAsyncT in /app/src/CAFlow.Common/Extensions/HttpClientExtensions.cs:line 44
at CAFlow.Core.Services.CodeFixApiService.GetIssuesAsync(String repoUrl, String commit, String branch) in /app/src/CAFlow.Core/Services/CodeFixApiService.cs:line 72
at CAFlow.Core.Services.CodeFixImportService.ImportIssuesAsync(Guid caFlowBranchId) in /app/src/CAFlow.Core/Services/CodeFixImportService.cs:line 75
at CAFlow.Core.Services.Pipeline.CodeFixImportExecutor.ExecuteAsync(PipelineContext context) in /app/src/CAFlow.Core/Services/Pipeline/CodeFixImportExecutor.cs:line 31
at CAFlow.Core.Models.PipelineContext.DoAsync(PipelineJobOperation operation) in /app/src/CAFlow.Core/Models/PipelineContext.cs:line 149
at CAFlow.Core.Models.PipelineContext.DoAsync(PipelineJobOperation operation) in /app/src/CAFlow.Core/Models/PipelineContext.cs:line 149
at CAFlow.Core.Services.PipelineService.ExecuteRepoAnalysisAsync(PipelineContext ctx) in /app/src/CAFlow.Core/Services/PipelineService.cs:line 181
at CAFlow.Core.Services.PipelineService.DoRepoAnalysisAsync(PipelineContext ctx) in /app/src/CAFlow.Core/Services/PipelineService.cs:line 139
at CAFlow.Core.Services.PipelineService.ExecuteAsync(CAPipelineInput input, Guid pipelineJobId, PerformContext performContext) in /app/src/CAFlow.Core/Services/PipelineService.cs:line 134
at CAFlow.Core.Services.PipelineService.ExecuteAsync(Guid pipelineJobId, PipelineQueue queue, PerformContext performContext) in /app/src/CAFlow.Core/Services/PipelineService.cs:line 57
at CAFlow.Core.Services.PipelineService.ExecuteAsync(Guid pipelineJobId, PipelineQueue queue, PerformContext performContext) in /app/src/CAFlow.Core/Services/PipelineService.cs:line 62
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
```
Hi guys I might be having a similar with the runtime docker image mcr.microsoft.com/dotnet/core/aspnet:2.1.16-alpine and my mac using netcore2.1 (2.1.16 runtime as well).
I noticed that my issue started appearing when I switched to IHttpClientFactory instead of a singleton HttpClient. I added my IHttpClientFactory through dependency injection with a timeout of 30 seconds and a primary message handler that ignores ssl certificates with the ServerCertificateCustomValidationCallback property.
I am calling another endpoint and the logs from the other endpoint show that it returned a response in about 1 second but I am still getting a TaskCancelled exception and the InnerException is a SocketException with the error messageUnable to read data from the transport connection: Operation canceled. and stack trace
at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error)
at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.GetResult(Int16 token)
at System.Net.Security.SslStreamInternal.<FillBufferAsync>g__InternalFillBufferAsync|38_0[TReadAdapter](TReadAdapter adap, ValueTask`1 task, Int32 min, Int32 initial)
at System.Threading.Tasks.ValueTask`1.get_Result()
at System.Net.Security.SslStreamInternal.ReadAsyncInternal[TReadAdapter](TReadAdapter adapter, Memory`1 buffer)
at System.Threading.Tasks.ValueTask`1.get_Result()
at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)
This happens randomly during the lifetime of the application. Most calls will be successful but there are a small number of calls that run into this issue. Would this be the same issue?
Playing around with a test project, I spun up 500 tasks in a for loop to make http GET calls an endpoint in a while loop. The endpoint has a random sleep between 500ms to 2000ms and returns 204. I got 2 different TaskCancelled Exceptions within less than a minute of the test running.
Other things I tried:
http instead of https made the error happen a lot less frequently. I also noticed that the stack trace was different so i posted it below public class HttpClientFactoryTestMinimal
{
public static async Task Test()
{
Console.WriteLine("Starting HttpClientFactoryMinimal Test");
var services = new ServiceCollection();
services.AddHttpClient(nameof(HttpClientFactoryTestMinimal))
.ConfigureHttpClient(c => c.Timeout = TimeSpan.FromSeconds(10))
.ConfigurePrimaryHttpMessageHandler(s =>
new HttpClientHandler
{
ServerCertificateCustomValidationCallback = (message, certificate2, arg3, arg4) => true
})
.SetHandlerLifetime(TimeSpan.FromSeconds(10))
;
var provider = services.BuildServiceProvider();
var factory = provider.GetRequiredService<IHttpClientFactory>();
var tasks = Enumerable.Range(0, Program.Threads).Select(x => Caller(factory));
await Task.WhenAll(tasks);
}
private static async Task Caller(IHttpClientFactory factory)
{
var num = 0;
while (true)
{
if(num++ % 20 == 0) Console.Write("h");
var corrId = Guid.NewGuid().ToString();
try
{
var request = new HttpRequestMessage
{
Method = HttpMethod.Get,
Headers =
{
{"x-correlation-id", corrId}
},
RequestUri = new Uri("https://some endpoint here")
};
var client = factory.CreateClient(nameof(HttpClientFactoryTestMinimal));
var resp = await Task.Run(() => client.SendAsync(request));
}
catch (Exception ex)
{
var error = "HttpClientFactory CorrelationId: " + corrId;
error += "\n HttpClientFactory ex.message: " + ex.Message;
error += '\n' + "HttpClientFactory ex.ToString(): " + ex.ToString();
error += '\n' + "HttpClientFactory ex.innerex.ToString(): " + ex.InnerException?.ToString();
Console.WriteLine(error);
Console.WriteLine();
}
}
}
}
For this exception I sometimes saw the other endpoint received the call and returned 204 within 2 seconds. Sometimes I didn't see the log on the other endpoint.
HttpClientFactory ex.message: The operation was canceled.
HttpClientFactory ex.ToString(): System.Threading.Tasks.TaskCanceledException: The operation was canceled. ---> System.IO.IOException: Unable to read data from the transport connection: Operation canceled. ---> System.Net.Sockets.SocketException: Operation canceled
--- End of inner exception stack trace ---
at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error)
at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.GetResult(Int16 token)
at System.Net.FixedSizeReader.ReadPacketAsync(Stream transport, AsyncProtocolRequest request)
at System.Net.Security.SslState.ThrowIfExceptional()
at System.Net.Security.SslState.InternalEndProcessAuthentication(LazyAsyncResult lazyResult)
at System.Net.Security.SslState.EndProcessAuthentication(IAsyncResult result)
at System.Net.Security.SslStream.EndAuthenticateAsClient(IAsyncResult asyncResult)
at System.Net.Security.SslStream.<>c.<AuthenticateAsClientAsync>b__47_1(IAsyncResult iar)
at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)
--- End of stack trace from previous location where exception was thrown ---
at System.Net.Http.ConnectHelper.EstablishSslConnectionAsyncCore(Stream stream, SslClientAuthenticationOptions sslOptions, CancellationToken cancellationToken)
--- End of inner exception stack trace ---
at System.Net.Http.ConnectHelper.EstablishSslConnectionAsyncCore(Stream stream, SslClientAuthenticationOptions sslOptions, CancellationToken cancellationToken)
at System.Threading.Tasks.ValueTask`1.get_Result()
at System.Net.Http.HttpConnectionPool.CreateConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Threading.Tasks.ValueTask`1.get_Result()
at System.Net.Http.HttpConnectionPool.WaitForCreatedConnectionAsync(ValueTask`1 creationTask)
at System.Threading.Tasks.ValueTask`1.get_Result()
at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at Microsoft.Extensions.Http.Logging.LoggingHttpMessageHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at Microsoft.Extensions.Http.Logging.LoggingScopeHttpMessageHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
at TestingStuff.HttpClientFactoryTestMinimal.Caller(IHttpClientFactory factory) in /Users/jnguyen/git/jnguyen/TestingStuff/TestingStuff/HttpClientFactoryTestMinimal.cs:line 52
HttpClientFactory ex.innerex.ToString(): System.IO.IOException: Unable to read data from the transport connection: Operation canceled. ---> System.Net.Sockets.SocketException: Operation canceled
--- End of inner exception stack trace ---
at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error)
at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.GetResult(Int16 token)
at System.Net.FixedSizeReader.ReadPacketAsync(Stream transport, AsyncProtocolRequest request)
at System.Net.Security.SslState.ThrowIfExceptional()
at System.Net.Security.SslState.InternalEndProcessAuthentication(LazyAsyncResult lazyResult)
at System.Net.Security.SslState.EndProcessAuthentication(IAsyncResult result)
at System.Net.Security.SslStream.EndAuthenticateAsClient(IAsyncResult asyncResult)
at System.Net.Security.SslStream.<>c.<AuthenticateAsClientAsync>b__47_1(IAsyncResult iar)
at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)
--- End of stack trace from previous location where exception was thrown ---
at System.Net.Http.ConnectHelper.EstablishSslConnectionAsyncCore(Stream stream, SslClientAuthenticationOptions sslOptions, CancellationToken cancellationToken)
For this exception I did not see the other endpoint logging this call. It has no inner exception it just says operation cancelled.
HttpClientFactory ex.message: The operation was canceled.
HttpClientFactory ex.ToString(): System.Threading.Tasks.TaskCanceledException: The operation was canceled.
at System.Net.Http.ConnectHelper.EstablishSslConnectionAsyncCore(Stream stream, SslClientAuthenticationOptions sslOptions, CancellationToken cancellationToken)
at System.Threading.Tasks.ValueTask`1.get_Result()
at System.Net.Http.HttpConnectionPool.CreateConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Threading.Tasks.ValueTask`1.get_Result()
at System.Net.Http.HttpConnectionPool.WaitForCreatedConnectionAsync(ValueTask`1 creationTask)
at System.Threading.Tasks.ValueTask`1.get_Result()
at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at Microsoft.Extensions.Http.Logging.LoggingHttpMessageHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at Microsoft.Extensions.Http.Logging.LoggingScopeHttpMessageHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
at TestingStuff.HttpClientFactoryTestMinimal.Caller(IHttpClientFactory factory) in /Users/jnguyen/git/jnguyen/TestingStuff/TestingStuff/HttpClientFactoryTestMinimal.cs:line 52
HttpClientFactory ex.innerex.ToString():
HttpClientFactory ex.message: The operation was canceled.
HttpClientFactory ex.ToString(): System.Threading.Tasks.TaskCanceledException: The operation was canceled. ---> System.IO.IOException: Unable to read data from the transport connection: Operation canceled. ---> System.Net.Sockets.SocketException: Operation canceled
--- End of inner exception stack trace ---
at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error)
at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.GetResult(Int16 token)
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 Microsoft.Extensions.Http.Logging.LoggingHttpMessageHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at Microsoft.Extensions.Http.Logging.LoggingScopeHttpMessageHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
at TestingStuff.HttpClientFactoryTestMinimal.Caller(IHttpClientFactory factory) in /Users/jnguyen/git/jnguyen/TestingStuff/TestingStuff/HttpClientFactoryTestMinimal.cs:line 52
HttpClientFactory ex.innerex.ToString(): System.IO.IOException: Unable to read data from the transport connection: Operation canceled. ---> System.Net.Sockets.SocketException: Operation canceled
--- End of inner exception stack trace ---
at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error)
at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.GetResult(Int16 token)
at System.Net.Http.HttpConnection.FillAsync()
at System.Net.Http.HttpConnection.ReadNextResponseHeaderLineAsync(Boolean foldedHeadersAllowed)
at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)
Triage: Thanks @HASSEN-MEDDEB-ATOS for opening the issue -- it seems we need more info debugging your scenario to make it actionable. Closing until we have that.
@asadcr @jonathann92 this is very general exception. Your cases may be valid, but it would be best to split them into separate issues and work there. It is hard to analyze different repros with potentially different causes on the same issue. Please open new issue with a simple repro and let's take it from there. Thanks!
I am encountering the same issue. I am porting an application from Windows to Linux.
The exception is raised immediately on performing the call, there is no waiting at all before it is cancelled.
The target server to access is absolutely accessible because we have the same application running at the same time on both Windows and Linux, and only the application on Linux fails. docker image mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim
Here is the exception, there is no InnerException
ERROR2020-07-31 08:09:50 - System.Threading.Tasks.TaskCanceledException: The operation was canceled.
at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.ConnectAsync(HttpRequestMessage request, Boolean allowHttp2, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.CreateHttp11ConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.GetHttpConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
at System.Net.Http.DecompressionHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
I have to use SocketsHttpHandler as below
_httpClient = new HttpClient(new SocketsHttpHandler()
{
UseProxy = false,
MaxConnectionsPerServer = 100,
AllowAutoRedirect = false,
SslOptions = new SslClientAuthenticationOptions()
{
RemoteCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true
},
AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip,
})
{
BaseAddress = uri,
Timeout = TimeSpan.FromSeconds(30),
};