Skip to content

Remove redundant service discovery registration in YARP configuration - #3007

Merged
MihaZupan merged 1 commit into
dotnet:mainfrom
halllo:NullReferenceException-in-ConfigurationServiceEndpointProviderOptionsValidator
Apr 8, 2026
Merged

MihaZupan merged 1 commit into
dotnet:mainfrom
halllo:NullReferenceException-in-ConfigurationServiceEndpointProviderOptionsValidator

Conversation

@halllo

@halllo halllo commented Apr 3, 2026

Copy link
Copy Markdown
Contributor

fix: remove redundant AddServiceDiscovery() call in Program.cs

Problem

The YARP gateway application crashes on startup with a NullReferenceException inside ConfigurationServiceEndpointProviderOptionsValidator, preventing MapReverseProxy() from completing. This manifests in Aspire 13.2.x with the mcr.microsoft.com/dotnet/nightly/yarp:2.3-preview image.

The root cause is a double registration of service discovery:

  • Extensions.cs:29 — AddServiceDefaults() already calls builder.Services.AddServiceDiscovery() internally
  • Program.cs:28 — AddServiceDiscovery() was called again immediately after
builder.AddServiceDefaults();           // ← registers AddServiceDiscovery() internally
builder.Services.AddServiceDiscovery(); // ← redundant second registration

In older versions of Microsoft.Extensions.ServiceDiscovery this was harmless — all internal registrations used TryAdd* patterns that silently no-op on duplicates. A version shipped with the Aspire 13.2.x image switched at least one registration to a non-idempotent Add* call, causing ConfigurationServiceEndpointProviderOptionsValidator to be registered twice with a factory whose dependency resolves to null on the second invocation.

Stack trace

Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
   at InvokeStub_ConfigurationServiceEndpointProviderOptionsValidator..ctor(Object, Object, IntPtr*)
   at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)
   ...
   at Microsoft.Extensions.DependencyInjection.ServiceDiscoveryServiceCollectionExtensions
         .<>c.<AddServiceDiscoveryCore>b__3_1(IServiceProvider sp)
   ...
   at Microsoft.AspNetCore.Builder.ReverseProxyIEndpointRouteBuilderExtensions.MapReverseProxy(...)
   at Program.<Main>$(String[] args) in /_/src/Application/Program.cs:line 40

Fix

Remove the redundant call from src/Application/Program.cs:

  builder.AddServiceDefaults();
- builder.Services.AddServiceDiscovery();
  builder.Services.AddReverseProxy()
      .LoadFromConfig(builder.Configuration.GetSection("ReverseProxy"))
      .AddServiceDiscoveryDestinationResolver();

Impact

  • Fixes the startup crash in Aspire 13.2.x / 2.3-preview images
  • No behavior change in prior versions — AddServiceDefaults() has always registered service discovery; the explicit call was always redundant
  • Eliminates the need for the arm64v8 image tag workaround

@MihaZupan MihaZupan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

@MihaZupan MihaZupan added this to the YARP 3.0 milestone Apr 8, 2026
@MihaZupan
MihaZupan merged commit 165278d into dotnet:main Apr 8, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants