.AddServiceDiscovery override any http client timeouts
- Dominant language
- C#
- Stars
- 6.3k
- Forks
- 991
- Avg merge
- 2d 15h
- Merged PRs (30d)
- 196
Description
### Is there an existing issue for this?
- [x] I have searched the existing issues
### Describe the bug
Hi,
I am not able for http client set timeout, however client.Timeout is 120 sec.
I tried direct change timeout, over timeout policy, nothing works.
As soo as I remove .AddServiceDiscovery() from http client setup and user hardcoded url, all works fine
and timeout was accepted.
example:
builder.Services.AddHttpClient("documentprocessingapi",
client =>
{
var url = config.GetValue("Api");
client.BaseAddress = new Uri(url);
client.Timeout = TimeSpan.FromSeconds(2000);
Log.Logger.Information("Set timeout to 120 sec");
})
//.AddPolicyHandler(timeoutPolicy)
.AddServiceDiscovery()
.ConfigurePrimaryHttpMessageHandler(() =>
{
var handler = new HttpClientHandler();
handler.ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator;
return handler;
})
.ConfigureHttpClient(client =>
{
client.Timeout = Timeout.InfiniteTimeSpan; // ⏳ Force unlimited timeout at HttpClient level
});
I am getting error:
2025-06-05 09:23:26.915 +02:00 [ERR] Resilience event occurred. EventName: 'OnTimeout', Source: '-standard//Standard-AttemptTimeout', Operation Key: 'null', Result: 'null'
2025-06-05 09:23:26.973 +02:00 [WRN] Execution attempt. Source: '-standard//Standard-Retry', Operation Key: 'null', Result: 'The operation didn't complete within the allowed timeout of '00:00:10'.', Handled: 'true', Attempt: '0', Execution Time: 10062.9676ms
Polly.Timeout.TimeoutRejectedException: The operation didn't complete within the allowed timeout of '00:00:10'.
---> System.Threading.Tasks.TaskCanceledException: The operation was canceled.
---> System.IO.IOException: Unable to read data from the transport connection: The I/O operation has been aborted because of either a thread exit or an application request..
---> System.Net.Sockets.SocketException (995): The I/O operation has been aborted because of either a thread exit or an application request.
--- End of inner exception stack trace ---
at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error, CancellationToken cancellationToken)
at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.System.Threading.Tasks.Sources.IValueTaskSource.GetResult(Int16 token)
at System.Net.Security.SslStream.EnsureFullTlsFrameAsync[TIOAdapter](CancellationToken cancellationToken, Int32 estimatedSize)
at System.Runtime.CompilerServices.PoolingAsyncValueTaskMethodBuilder`1.StateMachineBox`1.System.Threading.Tasks.Sources.IValueTaskSource.GetResult(Int16 token)
at System.Net.Security.SslStream.ReadAsyncInternal[TIOAdapter](Memory`1 buffer, CancellationToken cancellationToken)
at System.Runtime.CompilerServices.PoolingAsyncValueTaskMethodBuilder`1.StateMachineBox`1.System.Threading.Tasks.Sources.IValueTaskSource.GetResult(Int16 token)
at System.Net.Http.HttpConnection.InitialFillAsync(Boolean async)
at System.Net.Http.HttpConnection.SendAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
### Expected Behavior
_No response_
### Steps To Reproduce
_No response_
### Exceptions (if any)
_No response_
### .NET Version info
_No response_
### Anything else?
_No response_
Contributor guide
Assessment
This issue has not been assessed yet.