dotnet/yarp · error · ArgumentException

The consumer must implement at least one IMetricsConsumer in

Error message

The consumer must implement at least one IMetricsConsumer interface.

What it means

AddMetricsConsumer(object consumer) mirrors the telemetry overload but checks the IMetricsConsumer<TMetrics> interfaces (IMetricsConsumer<HttpClientMetrics>, IMetricsConsumer<SocketsMetrics>, IMetricsConsumer<NetSecurityMetrics>, ...). If the instance implements none, it throws ArgumentException(paramName: consumer) since there are no metrics to forward.

Source

Thrown at src/TelemetryConsumption/TelemetryConsumptionExtensions.cs:194

            services.TryAddEnumerable(ServiceDescriptor.Singleton(nameResolutionMetricsConsumer));
            implementsAny = true;
        }

        if (consumer is IMetricsConsumer<NetSecurityMetrics> netSecurityMetricsConsumer)
        {
            services.TryAddEnumerable(ServiceDescriptor.Singleton(netSecurityMetricsConsumer));
            implementsAny = true;
        }

        if (consumer is IMetricsConsumer<SocketsMetrics> socketsMetricsConsumer)
        {
            services.TryAddEnumerable(ServiceDescriptor.Singleton(socketsMetricsConsumer));
            implementsAny = true;
        }

        if (!implementsAny)
        {
            throw new ArgumentException("The consumer must implement at least one IMetricsConsumer interface.", nameof(consumer));
        }

        services.AddTelemetryListeners();

        return services;
    }

    /// <summary>
    /// Registers a consumer singleton for every IMetricsConsumer interface it implements.
    /// </summary>
    public static IServiceCollection AddMetricsConsumer<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TConsumer>(this IServiceCollection services)
        where TConsumer : class
    {
        var implementsAny = false;

        if (typeof(IMetricsConsumer<ForwarderMetrics>).IsAssignableFrom(typeof(TConsumer)))
        {
            services.AddSingleton(services => (IMetricsConsumer<ForwarderMetrics>)services.GetRequiredService<TConsumer>());

View on GitHub (pinned to bd11867bee)

Solutions

  1. Implement at least one recognized IMetricsConsumer<TMetrics> interface on the consumer.
  2. If the type only handles telemetry events, call AddTelemetryConsumer instead.
  3. Use the generic AddMetricsConsumer<TConsumer>() and confirm the closed IMetricsConsumer<> interface matches one YARP emits.

Example fix

// before
public class MyConsumer { ... }
services.AddMetricsConsumer(new MyConsumer());

// after
public class MyConsumer : IMetricsConsumer<HttpClientMetrics> { ... }
services.AddMetricsConsumer(new MyConsumer());
Defensive patterns

Strategy: type-guard

Validate before calling

static bool ImplementsAnyMetricsConsumer(object c) =>
    c is IMetricsConsumer<HttpClientMetrics>
    || c is IMetricsConsumer<SocketsMetrics>
    || c is IMetricsConsumer<NetSecurityMetrics>;

if (ImplementsAnyMetricsConsumer(consumer))
    services.AddMetricsConsumer(consumer);

Type guard

static bool ImplementsAnyMetricsConsumer(object c) =>
    c is IMetricsConsumer<HttpClientMetrics>
    || c is IMetricsConsumer<SocketsMetrics>
    || c is IMetricsConsumer<NetSecurityMetrics>;

Prevention

When it happens

Trigger: Calling services.AddMetricsConsumer(someObject) where someObject does not implement any IMetricsConsumer<...> interface, e.g. passing a telemetry-only consumer or a plain object.

Common situations: Using the metrics overload for a class that only implements I*TelemetryConsumer; refactor that removed the IMetricsConsumer interface; passing a consumer designed for a different metrics type that is not among the recognized ones.

Related errors


AI-assisted analysis of dotnet/yarp@bd11867bee (2026-08-13). Data as JSON: /api/errors/7c50480d9d2ebb1c. Report an issue: GitHub.