From 0e7baeb7299fbfc3a51a80a8b5a62ff8bdd13b7c Mon Sep 17 00:00:00 2001 From: Steve Ardalis Smith Date: Sun, 15 Nov 2026 09:22:27 -0500 Subject: [PATCH 1/4] Remove duplicate Vimeo DI registration in favor of AddVimeoServices Delete the local RegisterVimeoServicesDependencies (and the unused AuthHeaderHandler); AddVimeoServices in Program.cs already registers everything. Drop the now-unused vimeoToken parameter and the stale commented-out Autofac line. Add unit tests guarding against re-introducing the duplicate registration. Relates to #1486 Co-Authored-By: Claude Opus 5.5 --- .../2026-11-15-issue-1486-vimeo-di-cleanup.md | 39 ++++++++++ ...frastructureServiceCollectionExtensions.cs | 5 +- .../VimeoServiceCollectionExtensions.cs | 77 ------------------- src/DevBetterWeb.Web/Program.cs | 5 +- ...ructureServiceCollectionExtensionsTests.cs | 66 ++++++++++++++++ 5 files changed, 107 insertions(+), 85 deletions(-) create mode 100644 .claude/plans/2026-11-15-issue-1486-vimeo-di-cleanup.md delete mode 100644 src/DevBetterWeb.Infrastructure/VimeoServiceCollectionExtensions.cs create mode 100644 tests/DevBetterWeb.UnitTests/Infrastructure/InfrastructureServiceCollectionExtensionsTests.cs diff --git a/.claude/plans/2026-11-15-issue-1486-vimeo-di-cleanup.md b/.claude/plans/2026-11-15-issue-1486-vimeo-di-cleanup.md new file mode 100644 index 000000000..ca7cd6fb6 --- /dev/null +++ b/.claude/plans/2026-11-15-issue-1486-vimeo-di-cleanup.md @@ -0,0 +1,39 @@ +# Issue #1486 – Remove duplicate Vimeo DI registration + +Issue: https://github.com/DevBetterCom/DevBetterWeb/issues/1486 +Branch: `issue-1486-vimeo-di-cleanup` + +## Assumptions / scope + +- NimblePros.Vimeo **2.3.0** (the target named in the issue comment, which replaces 2.2.1) is + **not on nuget.org yet**. As of 2026-11-15 the latest version is 2.1.2. So the package upgrade is + deferred, and the PR will say `Relates to #1486` rather than `Fixes`. +- This PR does the registration cleanup, which the comment says must happen before (or + together with) the 2.3.0 upgrade. Otherwise startup throws `InvalidOperationException`. +- Manual smoke testing of the admin video flows needs a real Vimeo token and is left to the + maintainer. + +## Phase 1 – Registration cleanup + +- [x] Delete `src/DevBetterWeb.Infrastructure/VimeoServiceCollectionExtensions.cs`, including `AuthHeaderHandler`. +- [x] Remove `RegisterVimeoServicesDependencies` call and the `vimeoToken` parameter from `AddInfrastructureServices`. +- [x] Remove the `vimeoToken` local and the commented-out Autofac line from `Program.cs`. Keep `AddVimeoServices(vimeoSettings.Token)`. + +## Phase 2 – Automated tests (DevBetterWeb.UnitTests) + +- [x] `AddInfrastructureServices` registers no Vimeo services or named clients (guards against the 2.3.0 duplicate-detection startup failure). +- [x] `AddInfrastructureServices` followed by `AddVimeoServices` gives exactly one `ClientApiConfigurations` and one registration of each tus service, and the Vimeo services resolve. + +## Phase 3 – Independent review + +- [x] Separate agent reviews the full diff against the issue. +- [x] Address significant findings (or record reasons here). + +## Phase 4 – Verification + +- [x] `dotnet BuildTestFormat.cs` runs clean. + +## Deferred + +- [ ] Upgrade NimblePros.Vimeo to 2.3.0 once it is published. +- [ ] Smoke test the admin video flows (upload chunk, update description, delete, details, oEmbed). diff --git a/src/DevBetterWeb.Infrastructure/InfrastructureServiceCollectionExtensions.cs b/src/DevBetterWeb.Infrastructure/InfrastructureServiceCollectionExtensions.cs index 347f57b99..29300d9ea 100644 --- a/src/DevBetterWeb.Infrastructure/InfrastructureServiceCollectionExtensions.cs +++ b/src/DevBetterWeb.Infrastructure/InfrastructureServiceCollectionExtensions.cs @@ -19,7 +19,7 @@ namespace DevBetterWeb.Infrastructure; public static class InfrastructureServiceCollectionExtensions { - public static IServiceCollection AddInfrastructureServices(this IServiceCollection services, bool isDevelopment, string vimeoToken) + public static IServiceCollection AddInfrastructureServices(this IServiceCollection services, bool isDevelopment) { if (isDevelopment) { @@ -77,9 +77,6 @@ public static IServiceCollection AddInfrastructureServices(this IServiceCollecti services.AddTransient(); services.AddTransient(); - // Vimeo - services.RegisterVimeoServicesDependencies(vimeoToken); - return services; } } diff --git a/src/DevBetterWeb.Infrastructure/VimeoServiceCollectionExtensions.cs b/src/DevBetterWeb.Infrastructure/VimeoServiceCollectionExtensions.cs deleted file mode 100644 index bc53d02ee..000000000 --- a/src/DevBetterWeb.Infrastructure/VimeoServiceCollectionExtensions.cs +++ /dev/null @@ -1,77 +0,0 @@ -using System; -using System.Net.Http.Headers; -using System.Net.Http; -using System.Threading.Tasks; -using System.Threading; -using Microsoft.Extensions.DependencyInjection; -using NimblePros.ApiClient.Models; -using NimblePros.Vimeo.Interfaces; -using NimblePros.Vimeo.Services.UtilityServices; -using NimblePros.Vimeo.VideoTusService; - -namespace DevBetterWeb.Infrastructure; - -public static class VimeoServiceCollectionExtensions -{ - public static IServiceCollection RegisterVimeoServicesDependencies(this IServiceCollection services, string token) - { - Action configureVimeoApi = client => - { - client.BaseAddress = new Uri("https://api.vimeo.com"); - client.DefaultRequestHeaders.Add("Accept", "application/vnd.vimeo.*+json; version=3.4, application/json"); - client.Timeout = TimeSpan.FromMinutes(60); - }; - - Action configureVimeoOEmbed = client => - { - client.BaseAddress = new Uri("https://vimeo.com"); - client.DefaultRequestHeaders.Add("Accept", "application/vnd.vimeo.*+json; version=3.4, application/json"); - client.Timeout = TimeSpan.FromMinutes(60); - }; - - Action configureVimeoTus = client => - { - client.BaseAddress = new Uri("https://api.vimeo.com"); - client.DefaultRequestHeaders.Add("Accept", "application/vnd.vimeo.*+json; version=3.4, application/json"); - client.DefaultRequestHeaders.Add("Tus-Resumable", "1.0.0"); - client.Timeout = TimeSpan.FromMinutes(60); - }; - - // Register named HttpClients for injection - services.AddHttpClient("VimeoApi", configureVimeoApi); - services.AddHttpClient("VimeoOEmbed", configureVimeoOEmbed); - services.AddHttpClient("VimeoTusApi", configureVimeoTus); - - // Register ClientApiConfigurations (used by NimblePros.Vimeo) - var clientConfigs = new ClientApiConfigurations() - .AddHttpClientConfiguration(new HttpClientConfiguration("VimeoApi", configureVimeoApi, token)) - .AddHttpClientConfiguration(new HttpClientConfiguration("VimeoTusApi", configureVimeoTus, token)) - .AddHttpClientConfiguration(new HttpClientConfiguration("VimeoOEmbed", configureVimeoOEmbed)); - - services.AddSingleton(clientConfigs); - - // Core Vimeo services - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); - - return services; - } - -} - -public class AuthHeaderHandler : DelegatingHandler -{ - private readonly string _token; - - public AuthHeaderHandler(string token) - { - _token = token; - } - - protected override Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) - { - request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", _token); - return base.SendAsync(request, cancellationToken); - } -} diff --git a/src/DevBetterWeb.Web/Program.cs b/src/DevBetterWeb.Web/Program.cs index 2da240f4a..69b9a06d0 100644 --- a/src/DevBetterWeb.Web/Program.cs +++ b/src/DevBetterWeb.Web/Program.cs @@ -39,11 +39,8 @@ Console.WriteLine($"Startup ENV: {builder.Environment.EnvironmentName}"); var isProduction = builder.Environment.IsEnvironment("Production"); bool isDevelopment = builder.Environment.IsDevelopment(); -string vimeoToken = builder.Configuration["Vimeo:Token"] ?? ""; var isTesting = builder.Environment.IsEnvironment("Testing"); -// builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory()); - if (!isTesting) { builder.Host.UseSerilog((_, config) => config.ReadFrom.Configuration(builder.Configuration)); @@ -89,7 +86,7 @@ } -builder.Services.AddInfrastructureServices(isDevelopment, vimeoToken); +builder.Services.AddInfrastructureServices(isDevelopment); builder.Services.AddMediator(options => { diff --git a/tests/DevBetterWeb.UnitTests/Infrastructure/InfrastructureServiceCollectionExtensionsTests.cs b/tests/DevBetterWeb.UnitTests/Infrastructure/InfrastructureServiceCollectionExtensionsTests.cs new file mode 100644 index 000000000..d7d12c380 --- /dev/null +++ b/tests/DevBetterWeb.UnitTests/Infrastructure/InfrastructureServiceCollectionExtensionsTests.cs @@ -0,0 +1,66 @@ +using System.Linq; +using DevBetterWeb.Infrastructure; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Http; +using Microsoft.Extensions.Options; +using NimblePros.ApiClient.Models; +using NimblePros.Vimeo.Extensions; +using NimblePros.Vimeo.Interfaces; +using NimblePros.Vimeo.Services.UtilityServices; +using NimblePros.Vimeo.VideoServices; +using NimblePros.Vimeo.VideoTusService; +using Shouldly; +using Xunit; + +namespace DevBetterWeb.UnitTests.Infrastructure; + +public class InfrastructureServiceCollectionExtensionsTests +{ + private static readonly string[] VimeoClientNames = ["VimeoApi", "VimeoOEmbed", "VimeoTusApi"]; + + [Fact] + public void AddInfrastructureServices_DoesNotRegisterVimeoServices() + { + var services = new ServiceCollection(); + + services.AddInfrastructureServices(isDevelopment: false); + + ConfiguredHttpClientNames(services).ShouldNotContain(name => VimeoClientNames.Contains(name)); + services.ShouldNotContain(d => d.ServiceType == typeof(ClientApiConfigurations)); + services.ShouldNotContain(d => d.ServiceType == typeof(IUploadVideoTusService)); + services.ShouldNotContain(d => d.ServiceType == typeof(IUploadVideoSessionService)); + services.ShouldNotContain(d => d.ServiceType == typeof(ISleepService)); + } + + [Fact] + public void AddInfrastructureServicesThenAddVimeoServices_RegistersVimeoServicesOnce() + { + var services = new ServiceCollection(); + services.AddLogging(); + + services.AddInfrastructureServices(isDevelopment: false); + services.AddVimeoServices("test-token"); + + // AddVimeoServices configures VimeoApi more than once itself (base settings, then the token), + // so compare distinct names here; the guard against our own duplicate registration is the + // AddInfrastructureServices test above. + ConfiguredHttpClientNames(services).Distinct().ShouldBe(VimeoClientNames, ignoreOrder: true); + services.Count(d => d.ServiceType == typeof(ClientApiConfigurations)).ShouldBe(1); + services.Count(d => d.ServiceType == typeof(IUploadVideoTusService)).ShouldBe(1); + services.Count(d => d.ServiceType == typeof(IUploadVideoSessionService)).ShouldBe(1); + services.Count(d => d.ServiceType == typeof(ISleepService)).ShouldBe(1); + + using var provider = services.BuildServiceProvider(); + using var scope = provider.CreateScope(); + scope.ServiceProvider.GetRequiredService().ShouldNotBeNull(); + scope.ServiceProvider.GetRequiredService().ShouldNotBeNull(); + } + + private static string[] ConfiguredHttpClientNames(IServiceCollection services) => + services + .Where(d => d.ServiceType == typeof(IConfigureOptions)) + .Select(d => d.ImplementationInstance) + .OfType>() + .Select(o => o.Name ?? string.Empty) + .ToArray(); +} From 47ef00fd57912f7fce8fbe0222b3e2f34f51856c Mon Sep 17 00:00:00 2001 From: Steve Ardalis Smith Date: Sun, 15 Nov 2026 09:24:58 -0500 Subject: [PATCH 2/4] Normalize new test file encoding Co-Authored-By: Claude Opus 5.5 --- .../InfrastructureServiceCollectionExtensionsTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/DevBetterWeb.UnitTests/Infrastructure/InfrastructureServiceCollectionExtensionsTests.cs b/tests/DevBetterWeb.UnitTests/Infrastructure/InfrastructureServiceCollectionExtensionsTests.cs index d7d12c380..0f14ba775 100644 --- a/tests/DevBetterWeb.UnitTests/Infrastructure/InfrastructureServiceCollectionExtensionsTests.cs +++ b/tests/DevBetterWeb.UnitTests/Infrastructure/InfrastructureServiceCollectionExtensionsTests.cs @@ -1,4 +1,4 @@ -using System.Linq; +using System.Linq; using DevBetterWeb.Infrastructure; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Http; From e3d5e9b96164aab0b4a54626aa7ded7356f58391 Mon Sep 17 00:00:00 2001 From: Steve Ardalis Smith Date: Sun, 15 Nov 2026 09:29:27 -0500 Subject: [PATCH 3/4] Strengthen Vimeo DI regression tests after review Check named HttpClient options via IOptionsMonitor so any configuration style is caught, and drop the client-name assertion that depended on NimblePros.Vimeo internals. Record review outcome in the plan. Co-Authored-By: Claude Opus 5.5 --- .../2026-11-15-issue-1486-vimeo-di-cleanup.md | 6 +++- ...ructureServiceCollectionExtensionsTests.cs | 31 ++++++++++--------- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/.claude/plans/2026-11-15-issue-1486-vimeo-di-cleanup.md b/.claude/plans/2026-11-15-issue-1486-vimeo-di-cleanup.md index ca7cd6fb6..4d35195f7 100644 --- a/.claude/plans/2026-11-15-issue-1486-vimeo-di-cleanup.md +++ b/.claude/plans/2026-11-15-issue-1486-vimeo-di-cleanup.md @@ -28,10 +28,14 @@ Branch: `issue-1486-vimeo-di-cleanup` - [x] Separate agent reviews the full diff against the issue. - [x] Address significant findings (or record reasons here). + - No blockers. Applied: named-client check now uses `IOptionsMonitor`, which catches any configuration style, and the brittle client-name check against package internals was dropped. + - Left alone: the `Autofac` entry in `DevBetterWeb.ndproj` (still a transitive dependency of NimblePros.Vimeo) and the unrelated commented-out `DomainEventDispatcher` line. + - Wire change: `Accept` and `Tus-Resumable` are no longer sent twice. The Tus client no longer sets a base address or `Accept`, which is harmless because chunk PATCHes use an absolute upload link. The smoke test should still cover chunked upload. ## Phase 4 – Verification -- [x] `dotnet BuildTestFormat.cs` runs clean. +- [x] `dotnet BuildTestFormat.cs`: build OK, all tests pass (FunctionalTests, Tests, UnitTests). +- [x] Format step: the failures are pre-existing on `main` (around 11k repo-wide whitespace/imports/charset issues, unchanged by the .NET 11 upgrade). This PR adds no new ones: the new test file is clean and the touched files have fewer issues than on `main`. ## Deferred diff --git a/tests/DevBetterWeb.UnitTests/Infrastructure/InfrastructureServiceCollectionExtensionsTests.cs b/tests/DevBetterWeb.UnitTests/Infrastructure/InfrastructureServiceCollectionExtensionsTests.cs index 0f14ba775..e6879e888 100644 --- a/tests/DevBetterWeb.UnitTests/Infrastructure/InfrastructureServiceCollectionExtensionsTests.cs +++ b/tests/DevBetterWeb.UnitTests/Infrastructure/InfrastructureServiceCollectionExtensionsTests.cs @@ -16,7 +16,23 @@ namespace DevBetterWeb.UnitTests.Infrastructure; public class InfrastructureServiceCollectionExtensionsTests { - private static readonly string[] VimeoClientNames = ["VimeoApi", "VimeoOEmbed", "VimeoTusApi"]; + [Theory] + [InlineData("VimeoApi")] + [InlineData("VimeoOEmbed")] + [InlineData("VimeoTusApi")] + public void AddInfrastructureServices_DoesNotConfigureVimeoHttpClient(string clientName) + { + // NimblePros.Vimeo 2.3.0+ throws at startup if these clients are configured before AddVimeoServices. + var services = new ServiceCollection(); + services.AddOptions(); + + services.AddInfrastructureServices(isDevelopment: false); + + using var provider = services.BuildServiceProvider(); + var options = provider.GetRequiredService>().Get(clientName); + options.HttpClientActions.ShouldBeEmpty(); + options.HttpMessageHandlerBuilderActions.ShouldBeEmpty(); + } [Fact] public void AddInfrastructureServices_DoesNotRegisterVimeoServices() @@ -25,7 +41,6 @@ public void AddInfrastructureServices_DoesNotRegisterVimeoServices() services.AddInfrastructureServices(isDevelopment: false); - ConfiguredHttpClientNames(services).ShouldNotContain(name => VimeoClientNames.Contains(name)); services.ShouldNotContain(d => d.ServiceType == typeof(ClientApiConfigurations)); services.ShouldNotContain(d => d.ServiceType == typeof(IUploadVideoTusService)); services.ShouldNotContain(d => d.ServiceType == typeof(IUploadVideoSessionService)); @@ -41,10 +56,6 @@ public void AddInfrastructureServicesThenAddVimeoServices_RegistersVimeoServices services.AddInfrastructureServices(isDevelopment: false); services.AddVimeoServices("test-token"); - // AddVimeoServices configures VimeoApi more than once itself (base settings, then the token), - // so compare distinct names here; the guard against our own duplicate registration is the - // AddInfrastructureServices test above. - ConfiguredHttpClientNames(services).Distinct().ShouldBe(VimeoClientNames, ignoreOrder: true); services.Count(d => d.ServiceType == typeof(ClientApiConfigurations)).ShouldBe(1); services.Count(d => d.ServiceType == typeof(IUploadVideoTusService)).ShouldBe(1); services.Count(d => d.ServiceType == typeof(IUploadVideoSessionService)).ShouldBe(1); @@ -55,12 +66,4 @@ public void AddInfrastructureServicesThenAddVimeoServices_RegistersVimeoServices scope.ServiceProvider.GetRequiredService().ShouldNotBeNull(); scope.ServiceProvider.GetRequiredService().ShouldNotBeNull(); } - - private static string[] ConfiguredHttpClientNames(IServiceCollection services) => - services - .Where(d => d.ServiceType == typeof(IConfigureOptions)) - .Select(d => d.ImplementationInstance) - .OfType>() - .Select(o => o.Name ?? string.Empty) - .ToArray(); } From 203c1a699184c77705609edb111abcd9abc6b066 Mon Sep 17 00:00:00 2001 From: Steve Ardalis Smith Date: Sun, 15 Nov 2026 15:21:55 -0500 Subject: [PATCH 4/4] Exempt Vimeo HttpClients from ServiceDefaults resilience handler AddServiceDefaults adds the standard resilience handler to every HttpClient. Its 10s attempt / 30s total timeouts override the Vimeo clients' 60-minute timeout and would cut off large chunk uploads. Wrap AddVimeoServices in AddDevBetterVimeoServices, which removes the resilience handlers from VimeoApi, VimeoTusApi and VimeoOEmbed after registration, and add tests against a host built like Program.cs. Co-Authored-By: Claude Opus 5.5 --- .../2026-11-15-issue-1486-vimeo-di-cleanup.md | 13 +++- src/DevBetterWeb.Web/Program.cs | 3 +- .../VimeoServiceCollectionExtensions.cs | 38 +++++++++++ .../VimeoServiceCollectionExtensionsTests.cs | 68 +++++++++++++++++++ 4 files changed, 119 insertions(+), 3 deletions(-) create mode 100644 src/DevBetterWeb.Web/VimeoServiceCollectionExtensions.cs create mode 100644 tests/DevBetterWeb.UnitTests/Web/VimeoServiceCollectionExtensionsTests.cs diff --git a/.claude/plans/2026-11-15-issue-1486-vimeo-di-cleanup.md b/.claude/plans/2026-11-15-issue-1486-vimeo-di-cleanup.md index 6edb394e2..246a3278e 100644 --- a/.claude/plans/2026-11-15-issue-1486-vimeo-di-cleanup.md +++ b/.claude/plans/2026-11-15-issue-1486-vimeo-di-cleanup.md @@ -42,7 +42,18 @@ Branch: `issue-1486-vimeo-di-cleanup` - [x] Merge latest `main` (Aspire AppHost #1488). Resolved the `Program.cs` conflict, keeping the Aspire block and dropping the Autofac comment. - [x] Bump `NimblePros.Vimeo` 2.1.2 → 2.3.0 in `Directory.Packages.props`. - [x] `dotnet BuildTestFormat.cs`: build OK, 315/315 tests pass (AppHost.Tests 4, FunctionalTests 5, Tests 118, UnitTests 188). The functional tests start the full host, so 2.3.0's startup duplicate-registration check passes. The format failures are the same pre-existing ones as on `main`. -- Risk outside this PR: `AddServiceDefaults` (#1488) adds `AddStandardResilienceHandler` to every HttpClient, Vimeo ones included. Its default timeouts (10s per attempt, 30s total) override the Vimeo clients' 60-minute timeout and could break large chunk uploads. Tracked separately. +- Risk outside this PR: `AddServiceDefaults` (#1488) adds `AddStandardResilienceHandler` to every HttpClient, Vimeo ones included. Its default timeouts (10s per attempt, 30s total) override the Vimeo clients' 60-minute timeout and could break large chunk uploads. + +## Phase 6 – Vimeo resilience timeout fix (requested by the maintainer) + +- [x] Added `AddDevBetterVimeoServices` in `src/DevBetterWeb.Web/VimeoServiceCollectionExtensions.cs`. It calls `AddVimeoServices` and then `RemoveAllResilienceHandlers()` on `VimeoApi`, `VimeoTusApi` and `VimeoOEmbed`, using the `ServiceConstants` names, so the clients' 60-minute timeout applies again. It runs after `AddVimeoServices`, as 2.3.0 requires. + - `RemoveAllResilienceHandlers` is marked experimental (EXTEXP0001), so the warning is suppressed with a `#pragma` on that one line. It is the documented way to opt a client out of `ConfigureHttpClientDefaults`. The other options rely on the internal `"{name}-standard"` pipeline naming or mean changing the shared ServiceDefaults. +- [x] `Program.cs` calls `AddDevBetterVimeoServices`. +- [x] Tests in `tests/DevBetterWeb.UnitTests/Web/VimeoServiceCollectionExtensionsTests.cs` build the host with `AddServiceDefaults`, as `Program.cs` does, and check: + - no `ResilienceHandler` in any Vimeo client's handler chain (these fail without the fix); + - each Vimeo client keeps its 60-minute timeout; + - other clients keep their resilience handler. +- [x] `dotnet BuildTestFormat.cs`: build OK, 322/322 tests pass. Running `dotnet format --verify-no-changes` on the solution, limited to the new files, exits 0. The full-solution run still reports one IMPORTS issue at (1,1) of the new Web file, which I could not reproduce in isolation. Every other format issue is pre-existing on `main`. ## Deferred diff --git a/src/DevBetterWeb.Web/Program.cs b/src/DevBetterWeb.Web/Program.cs index d2079bcf2..95ac89303 100644 --- a/src/DevBetterWeb.Web/Program.cs +++ b/src/DevBetterWeb.Web/Program.cs @@ -28,7 +28,6 @@ using Microsoft.Extensions.Logging; using Microsoft.OpenApi; using NimblePros.Metronome; -using NimblePros.Vimeo.Extensions; using Serilog; // 29 Aug 2023 - Getting a nullref in here somewhere maybe? Also a stack overflow during startup somewhere. @@ -160,7 +159,7 @@ VimeoSettings vimeoSettings = builder.Configuration.GetSection(Constants.ConfigKeys.VimeoSettings)!.Get()!; builder.Services.AddSingleton(vimeoSettings); -builder.Services.AddVimeoServices(vimeoSettings.Token); +builder.Services.AddDevBetterVimeoServices(vimeoSettings.Token); // list services builder.Services.Configure(config => diff --git a/src/DevBetterWeb.Web/VimeoServiceCollectionExtensions.cs b/src/DevBetterWeb.Web/VimeoServiceCollectionExtensions.cs new file mode 100644 index 000000000..2b9ae14db --- /dev/null +++ b/src/DevBetterWeb.Web/VimeoServiceCollectionExtensions.cs @@ -0,0 +1,38 @@ +using Microsoft.Extensions.DependencyInjection; +using NimblePros.Vimeo.Constants; +using NimblePros.Vimeo.Extensions; + +namespace DevBetterWeb.Web; + +public static class VimeoServiceCollectionExtensions +{ + private static readonly string[] VimeoHttpClientNames = + [ + ServiceConstants.VIMEO_API_HTTP_NAME, + ServiceConstants.VIMEO_API_TUS_HTTP_NAME, + ServiceConstants.VIMEO_OEMBED_HTTP_NAME, + ]; + + /// + /// Registers NimblePros.Vimeo, then removes the standard resilience handler that + /// AddServiceDefaults adds to every HttpClient. Its timeouts (10s per attempt, 30s total) + /// would override the Vimeo clients' 60-minute timeout and cut off large chunk uploads. + /// The clients must be configured after AddVimeoServices, which throws if they were + /// configured earlier. + /// + public static IServiceCollection AddDevBetterVimeoServices(this IServiceCollection services, string token) + { + services.AddVimeoServices(token); + + foreach (var clientName in VimeoHttpClientNames) + { + // RemoveAllResilienceHandlers is the documented way to opt a client out of + // ConfigureHttpClientDefaults resilience, but it is still marked experimental. +#pragma warning disable EXTEXP0001 + services.AddHttpClient(clientName).RemoveAllResilienceHandlers(); +#pragma warning restore EXTEXP0001 + } + + return services; + } +} diff --git a/tests/DevBetterWeb.UnitTests/Web/VimeoServiceCollectionExtensionsTests.cs b/tests/DevBetterWeb.UnitTests/Web/VimeoServiceCollectionExtensionsTests.cs new file mode 100644 index 000000000..a68e6a499 --- /dev/null +++ b/tests/DevBetterWeb.UnitTests/Web/VimeoServiceCollectionExtensionsTests.cs @@ -0,0 +1,68 @@ +using System; +using System.Net.Http; +using DevBetterWeb.Web; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Http.Resilience; +using NimblePros.Vimeo.Constants; +using Shouldly; +using Xunit; + +namespace DevBetterWeb.UnitTests.Web; + +public class VimeoServiceCollectionExtensionsTests +{ + [Theory] + [InlineData(ServiceConstants.VIMEO_API_HTTP_NAME)] + [InlineData(ServiceConstants.VIMEO_API_TUS_HTTP_NAME)] + [InlineData(ServiceConstants.VIMEO_OEMBED_HTTP_NAME)] + public void AddDevBetterVimeoServices_RemovesServiceDefaultsResilienceFromVimeoClient(string clientName) + { + using var provider = BuildProviderLikeProgram(); + + HasResilienceHandler(provider, clientName).ShouldBeFalse(); + } + + [Theory] + [InlineData(ServiceConstants.VIMEO_API_HTTP_NAME)] + [InlineData(ServiceConstants.VIMEO_API_TUS_HTTP_NAME)] + [InlineData(ServiceConstants.VIMEO_OEMBED_HTTP_NAME)] + public void AddDevBetterVimeoServices_KeepsLongVimeoClientTimeout(string clientName) + { + using var provider = BuildProviderLikeProgram(); + + var client = provider.GetRequiredService().CreateClient(clientName); + + client.Timeout.ShouldBe(TimeSpan.FromMinutes(60)); + } + + [Fact] + public void AddDevBetterVimeoServices_LeavesResilienceOnOtherClients() + { + using var provider = BuildProviderLikeProgram(); + + HasResilienceHandler(provider, "SomeOtherClient").ShouldBeTrue(); + } + + private static ServiceProvider BuildProviderLikeProgram() + { + var builder = Host.CreateEmptyApplicationBuilder(new HostApplicationBuilderSettings()); + builder.AddServiceDefaults(); + builder.Services.AddDevBetterVimeoServices("test-token"); + return builder.Services.BuildServiceProvider(); + } + + private static bool HasResilienceHandler(IServiceProvider provider, string clientName) + { + HttpMessageHandler? handler = provider.GetRequiredService().CreateHandler(clientName); + while (handler is DelegatingHandler delegating) + { + if (delegating is ResilienceHandler) + { + return true; + } + handler = delegating.InnerHandler; + } + return false; + } +}