dotnet/yarp · error · ArgumentException

A consumer may not be null

Error message

A consumer may not be null

What it means

EventListenerService (the telemetry-consumption event listener) rejects any null element inside the telemetry or metrics consumer enumerables passed to its constructor. After copying the sequences to arrays it scans for nulls and throws ArgumentException, naming whichever collection (telemetryConsumers or metricsConsumers) contained the null. This protects the listener loop, which would otherwise NullReferenceException on each event.

Source

Thrown at src/TelemetryConsumption/EventListenerService.cs:50

    private readonly object _syncObject = new();
    private readonly bool _initialized;

    public EventListenerService(
        ILogger<TService> logger,
        IEnumerable<TTelemetryConsumer> telemetryConsumers,
        IEnumerable<IMetricsConsumer<TMetrics>> metricsConsumers)
    {
        ArgumentNullException.ThrowIfNull(logger);
        ArgumentNullException.ThrowIfNull(telemetryConsumers);
        ArgumentNullException.ThrowIfNull(metricsConsumers);

        _logger = logger;
        _telemetryConsumers = telemetryConsumers.ToArray();
        _metricsConsumers = metricsConsumers.ToArray();

        if (_telemetryConsumers.Any(s => s is null) || metricsConsumers.Any(c => c is null))
        {
            throw new ArgumentException("A consumer may not be null",
                _telemetryConsumers.Any(s => s is null) ? nameof(telemetryConsumers) : nameof(metricsConsumers));
        }

        if (_telemetryConsumers.Length == 0)
        {
            _telemetryConsumers = null;
        }

        if (_metricsConsumers.Length == 0)
        {
            _metricsConsumers = null;
        }

        lock (_syncObject)
        {
            if (_eventSource is EventSource eventSource)
            {
                EnableEventSource(eventSource);

View on GitHub (pinned to bd11867bee)

Solutions

  1. Ensure every registered consumer factory returns a non-null instance; use null-conditional registration or guard the factory.
  2. Filter nulls out of any manually constructed consumer collection before registering.
  3. If a consumer is optional, register nothing rather than registering null.
  4. In tests, replace null consumer stubs with real no-op implementations.

Example fix

// before
services.AddSingleton<ITelemetryConsumer<HttpRequestTelemetry>>(_ => enabled ? new MyConsumer() : null);

// after
if (enabled) {
    services.AddSingleton<ITelemetryConsumer<HttpRequestTelemetry>>(new MyConsumer());
}
Defensive patterns

Strategy: validation

Validate before calling

// Filter nulls before registering consumers.
var consumers = new ITelemetryConsumer<HttpRequestTelemetry>[] { a, b, c }
    .Where(c => c is not null).ToArray();
foreach (var c in consumers)
    services.AddSingleton(c);

Prevention

When it happens

Trigger: Registering a consumer via AddTelemetryConsumer/AddMetricsConsumer (or direct DI) whose factory resolves to null, e.g. services.AddSingleton<ITelemetryConsumer<...>>(_ => null), or passing a params/array that contains a null entry. The null propagates into the EventListenerService constructor and trips the check.

Common situations: A DI factory returning null under a feature flag; a conditional AddSingleton that was meant to skip registration but instead registers null; manually building the consumer list and including an unset variable; a broken mock in tests registering null.

Related errors


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