Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions .claude/plans/2026-11-15-issue-1486-vimeo-di-cleanup.md
Original file line number Diff line number Diff line change
@@ -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<HttpClientFactoryOptions>`, 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).
2 changes: 1 addition & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
<PackageVersion Include="Newtonsoft.Json" Version="13.0.4" />
<PackageVersion Include="NimblePros.ApiClient" Version="2.2.0" />
<PackageVersion Include="NimblePros.Metronome" Version="0.4.1" />
<PackageVersion Include="NimblePros.Vimeo" Version="2.1.2" />
<PackageVersion Include="NimblePros.Vimeo" Version="2.3.0" />
<PackageVersion Include="NSubstitute" Version="6.2.0" />
<PackageVersion Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.21.0" />
<PackageVersion Include="OpenTelemetry.Extensions.Hosting" Version="1.21.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -77,9 +77,6 @@ public static IServiceCollection AddInfrastructureServices(this IServiceCollecti
services.AddTransient<IInvoiceHandlerListService, StripeInvoiceHandlerListService>();
services.AddTransient<ISubscriptionHandlerService, StripeSubscriptionHandlerService>();

// Vimeo
services.RegisterVimeoServicesDependencies(vimeoToken);

return services;
}
}
8 changes: 2 additions & 6 deletions src/DevBetterWeb.Web/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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),
Expand Down Expand Up @@ -104,7 +100,7 @@

}

builder.Services.AddInfrastructureServices(isDevelopment, vimeoToken);
builder.Services.AddInfrastructureServices(isDevelopment);

builder.Services.AddMediator(options =>
{
Expand Down Expand Up @@ -163,7 +159,7 @@

VimeoSettings vimeoSettings = builder.Configuration.GetSection(Constants.ConfigKeys.VimeoSettings)!.Get<VimeoSettings>()!;
builder.Services.AddSingleton(vimeoSettings);
builder.Services.AddVimeoServices(vimeoSettings.Token);
builder.Services.AddDevBetterVimeoServices(vimeoSettings.Token);

// list services
builder.Services.Configure<ServiceConfig>(config =>
Expand Down
38 changes: 38 additions & 0 deletions src/DevBetterWeb.Web/VimeoServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -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,
];

/// <summary>
/// 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.
/// </summary>
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;
}
}
Original file line number Diff line number Diff line change
@@ -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<IOptionsMonitor<HttpClientFactoryOptions>>().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<GetVideoService>().ShouldNotBeNull();
scope.ServiceProvider.GetRequiredService<IUploadVideoTusService>().ShouldNotBeNull();
}
}
Original file line number Diff line number Diff line change
@@ -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<IHttpClientFactory>().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<IHttpMessageHandlerFactory>().CreateHandler(clientName);
while (handler is DelegatingHandler delegating)
{
if (delegating is ResilienceHandler)
{
return true;
}
handler = delegating.InnerHandler;
}
return false;
}
}
Loading