dotnet/reactive · error · ArgumentNullException

ArgumentNullException(nameof(observer))

Error message

ArgumentNullException(nameof(observer))

What it means

Do<TSource>(this IAsyncObservable<TSource> source, IObserver<TSource> observer) throws ArgumentNullException when the IObserver<TSource> tap observer is null. The observer receives the mirrored OnNext/OnError/OnCompleted notifications, and a null observer would fail only at first notification, so the library rejects it eagerly.

Solutions

  1. Pass a real IObserver<TSource> implementation (e.g. an AnonymousObserver or custom logger observer).
  2. If the tap observer is optional, branch to a Do-free pipeline instead of passing null.
  3. Verify DI registration/configuration for the observer dependency.

Example fix

// before
source.Do(tapObserver); // tapObserver null
// after
var tapped = tapObserver != null ? source.Do(tapObserver) : source;
Defensive patterns

Strategy: validation

Validate before calling

if (tapObserver is null) throw new InvalidOperationException("tap observer required");
var tapped = source.Do(tapObserver);

Type guard

static bool IsValidTap<TSource>(IObserver<TSource>? o) => o is not null;

Try / catch

try { var tapped = source.Do(observer); }
catch (ArgumentNullException ex) when (ex.ParamName == "observer") { /* construct or register the tap observer */ }

Prevention

When it happens

Trigger: Calling source.Do(null) or AsyncObservable.Do(source, null) with the IObserver<TSource> overload.

Common situations: A logging/tap observer resolved from DI that was not registered; a nullable observer field used for testing; conditional creation of the spy observer skipped by a branch.

Related errors


AI-assisted analysis of dotnet/reactive@94b5d5ab91 (2026-09-15). Data as JSON: /api/errors/60083afe372de629. Report an issue: GitHub.

Appendix: source

Thrown at AsyncRx.NET/System.Reactive.Async/Linq/Operators/Do.cs:16

// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT License.
// See the LICENSE file in the project root for more information. 

using System.Threading.Tasks;

namespace System.Reactive.Linq
{
    public partial class AsyncObservable
    {
        public static IAsyncObservable<TSource> Do<TSource>(this IAsyncObservable<TSource> source, IObserver<TSource> observer)
        {
            if (source == null)
                throw new ArgumentNullException(nameof(source));
            if (observer == null)
                throw new ArgumentNullException(nameof(observer));

            return Create(
                source,
                observer,
                static (source, observer, target) => source.SubscribeSafeAsync(AsyncObserver.Do(target, observer)));
        }

        public static IAsyncObservable<TSource> Do<TSource>(this IAsyncObservable<TSource> source, Action<TSource> onNext)
        {
            if (source == null)
                throw new ArgumentNullException(nameof(source));
            if (onNext == null)
                throw new ArgumentNullException(nameof(onNext));

            return Create(
                source,
                onNext,
                static (source, onNext, target) => source.SubscribeSafeAsync(AsyncObserver.Do(target, onNext)));

View on GitHub (pinned to 94b5d5ab91)