extensions: Currently impossible to override the AddStandardResillienceHandler() call for a specific client which means that you either use it or can't.

Description

I debated if this was a bug or a feature request, but since Aspire sets this by the default (which it should) and then every single HttpClient you create then is forced into those defaults with apparently no way to override them, this becomes a bug because it’s both not discoverable with what’s happening and the documentation says nothing about this, while simultaneously adding a new resilience handler to a specific client implementation doesn’t override it.

Hence, at best, it’s a bug in documentation, but more, this is a major blocker, because it’s impossible to set anything other than defaults as soon as you opt into this, which I can’t imagine was the design intent.

Reproduction Steps

If this is in your project.

		builder.Services.ConfigureHttpClientDefaults(http => {
			//Turn on resilience by default
			http.AddStandardResilienceHandler();

			// Turn on service discovery by default
			http.UseServiceDiscovery();
		});

The timeout on any client that you try and override will be ignored.

This doesn’t work.

services.AddHttpClient<IKaiLLamaClient, LLamaKaiLLM>()
.AddStandardResilienceHandler(options => {
	options.AttemptTimeout = new HttpTimeoutStrategyOptions {
		Timeout = TimeSpan.FromMinutes(5)
	};
	options.TotalRequestTimeout = new HttpTimeoutStrategyOptions {
		Timeout = TimeSpan.FromMinutes(15)
	};
	options.CircuitBreaker.SamplingDuration = TimeSpan.FromMinutes(10);
});

This doesn’t work: (where timeoutPolicy is a polly timeout policy that sets the time out)

builder.Services.AddHttpClient<IOrca2LLamaClient, LLamaOrca2LLM>(client => {
	client.Timeout = TimeSpan.FromMinutes(5);
})
.AddPolicyHandler(timeoutPolicy);

I’ve tried every permutation and combination I can think of, and in all cases, it is ignored.

Expected behavior

You should be able to override the defaults on a specific client and how to do so should be documented clearly.

This should just work:

			services.AddHttpClient<IKaiLLamaClient, LLamaKaiLLM>()
				.AddStandardResilienceHandler(options => {
					options.AttemptTimeout = new HttpTimeoutStrategyOptions {
						Timeout = TimeSpan.FromMinutes(5)
					};
					options.TotalRequestTimeout = new HttpTimeoutStrategyOptions {
						Timeout = TimeSpan.FromMinutes(15)
					};
					options.CircuitBreaker.SamplingDuration = TimeSpan.FromMinutes(10);
				});

Or there should be an OverrideResilienceHandler that replaces it for this client or something.

Actual behavior

It appears to just add a new resiliance handler that is suplimental to the other one, so whatever one times out first, kills the client.

Regression?

No response

Known Workarounds

None other than removing all default resiliance handling for http.

Configuration

.NET SDK: Version: 8.0.100 Commit: 57efcf1350 Workload version: 8.0.100-manifests.71b9f198

Runtime Environment: OS Name: Windows OS Version: 10.0.22631 OS Platform: Windows RID: win-x64 Base Path: C:\Program Files\dotnet\sdk\8.0.100\

.NET workloads installed: Workload version: 8.0.100-manifests.71b9f198 [aspire] Installation Source: SDK 8.0.100, VS 17.9.34321.82 Manifest Version: 8.0.0-preview.1.23557.2/8.0.100 Manifest Path: C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.aspire\8.0.0-preview.1.23557.2\WorkloadManifest.json Install Type: Msi

[maui-windows] Installation Source: VS 17.8.34330.188 Manifest Version: 8.0.3/8.0.100 Manifest Path: C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.maui\8.0.3\WorkloadManifest.json Install Type: Msi

[maccatalyst] Installation Source: VS 17.8.34330.188 Manifest Version: 17.0.8478/8.0.100 Manifest Path: C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.maccatalyst\17.0.8478\WorkloadManifest.json Install Type: Msi

[ios] Installation Source: VS 17.8.34330.188 Manifest Version: 17.0.8478/8.0.100 Manifest Path: C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.ios\17.0.8478\WorkloadManifest.json Install Type: Msi

[android] Installation Source: VS 17.8.34330.188 Manifest Version: 34.0.43/8.0.100 Manifest Path: C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.android\34.0.43\WorkloadManifest.json
Install Type: Msi

Host: Version: 8.0.0 Architecture: x64 Commit: 5535e31a71

.NET SDKs installed: 6.0.417 [C:\Program Files\dotnet\sdk] 7.0.404 [C:\Program Files\dotnet\sdk] 8.0.100 [C:\Program Files\dotnet\sdk]

.NET runtimes installed: Microsoft.AspNetCore.App 6.0.25 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 7.0.14 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 8.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 6.0.25 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 7.0.14 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 8.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.WindowsDesktop.App 6.0.25 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 7.0.14 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 8.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Other architectures found: x86 [C:\Program Files (x86)\dotnet] registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]

Environment variables: Not set

global.json file: E:\Repos\Atteria\Pltfrmd\services\global.json

Learn more: https://aka.ms/dotnet/info

Download .NET: https://aka.ms/dotnet/download

Other information

No response

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 13
  • Comments: 16 (8 by maintainers)

Most upvoted comments

Hello, My team is responsible for internal tools and libraries in the company I work in. We recently provided a lib which added a standard resilience handler to the httpclientdefaultbuilder

services.AddHttpClient()
        .ConfigureHttpClientDefaults(defaultBuilder =>
        {
            defaultBuilder.AddStandardResilienceHandler();
        });

One of the first feedback we had, was how to disable it or tune it. We ended up providing an extension method :

/// <summary>
/// Provider extension methods to <see cref="IHttpClientBuilder"/>
/// </summary>
public static class HttpClientBuilderExtensions
{
#pragma warning disable EXTEXP0001
    /// <summary>
    /// Remove already configured resilience handlers
    /// </summary>
    /// <param name="builder">The builder instance.</param>
    /// <returns>The value of <paramref name="builder" />.</returns>
    public static IHttpClientBuilder ClearResilienceHandlers(this IHttpClientBuilder builder)
    {
        builder.ConfigureAdditionalHttpMessageHandlers(static (handlers, _) =>
        {
            for (int i = 0; i < handlers.Count;)
            {
                if (handlers[i] is ResilienceHandler)
                {
                    handlers.RemoveAt(i);
                    continue;
                }
                i++;
            }
        });
        return builder;
    }
#pragma warning restore EXTEXP0001
}

Our devs are now able to do this:

builder.Services
    .AddHttpClient("Test")
    .ClearResilienceHandlers()
    .AddStandardHedgingHandler();

Hope this helps !

Additionally to what @JohnGalt1717 suggests: It would also help us if we could override the resiliency settings for one single endpoint of a typed http client.

The use case for this is that we have a typed http client with some endpoints where we want to use the standard resiliency settings. But we have one single endpoint in that typed http client where we know that this endpoint takes longer than the configured AttemptTimeout (which is totally fine). We have no possibility - as I’m aware of - to override the default behavior for this single endpoint.

@NatMarchand This is an expected workaround, but I cannot agree with the comment above that this is awesome. It’s terrible, especially because you have to ignore the warning and that this type might be removed
I hope for a clearer approach 😔

builder.Services
    .AddHttpClient("Test")
    .ClearResilienceHandlers()
    .AddStandardHedgingHandler();

No, it won’t. It will remove the handler registered as default only for the Test HttpClient.

@JohnGalt1717 The issue you reported is being discussed https://github.com/dotnet/extensions/issues/5021. Could you, please, take a look at the following comment https://github.com/dotnet/extensions/issues/5021#issuecomment-2359380710 and share your feedback? Thank you.