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 00000000..246a3278 --- /dev/null +++ b/.claude/plans/2026-11-15-issue-1486-vimeo-di-cleanup.md @@ -0,0 +1,60 @@ +# 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). + - 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`: 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`. + +## Phase 5 – Package upgrade (2.3.0 published later on 2026-11-15) + +- [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. + +## 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 + +- [ ] Smoke test the admin video flows (upload chunk, update description, delete, details, oEmbed). diff --git a/Directory.Packages.props b/Directory.Packages.props index a0e928c2..23d2e082 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -51,7 +51,7 @@ - + diff --git a/src/DevBetterWeb.Infrastructure/InfrastructureServiceCollectionExtensions.cs b/src/DevBetterWeb.Infrastructure/InfrastructureServiceCollectionExtensions.cs index 347f57b9..29300d9e 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.Web/Program.cs b/src/DevBetterWeb.Web/Program.cs index a6172315..95ac8930 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. @@ -39,7 +38,6 @@ 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"); // When running under Aspire (an OTLP endpoint is configured), forward Serilog events to the @@ -55,8 +53,6 @@ // Aspire service defaults: OpenTelemetry, health checks, resilience, service discovery builder.AddServiceDefaults(); -// builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory()); - if (!isTesting) { builder.Host.UseSerilog((_, config) => config.ReadFrom.Configuration(builder.Configuration), @@ -104,7 +100,7 @@ } -builder.Services.AddInfrastructureServices(isDevelopment, vimeoToken); +builder.Services.AddInfrastructureServices(isDevelopment); builder.Services.AddMediator(options => { @@ -163,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 00000000..2b9ae14d --- /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/Infrastructure/InfrastructureServiceCollectionExtensionsTests.cs b/tests/DevBetterWeb.UnitTests/Infrastructure/InfrastructureServiceCollectionExtensionsTests.cs new file mode 100644 index 00000000..e6879e88 --- /dev/null +++ b/tests/DevBetterWeb.UnitTests/Infrastructure/InfrastructureServiceCollectionExtensionsTests.cs @@ -0,0 +1,69 @@ +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 +{ + [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() + { + var services = new ServiceCollection(); + + services.AddInfrastructureServices(isDevelopment: false); + + 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"); + + 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(); + } +} diff --git a/tests/DevBetterWeb.UnitTests/Web/VimeoServiceCollectionExtensionsTests.cs b/tests/DevBetterWeb.UnitTests/Web/VimeoServiceCollectionExtensionsTests.cs new file mode 100644 index 00000000..a68e6a49 --- /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; + } +}