dotnet/reactive · error · ArgumentNullException

ArgumentNullException: source

Error message

ArgumentNullException: source

What it means

Observable.Replay(source, scheduler) throws ArgumentNullException with ParamName "source" when the source IObservable is null. This overload takes both a source and an IScheduler; the source null-check happens first (line ~445), before the scheduler check. Rx throws eagerly at argument-validation time.

Solutions

  1. Supply a non-null IObservable<TSource> as the first argument.
  2. Fix the code that produced the null source (factory, DI, config lookup).
  3. Add a pre-call null guard so the failure surfaces at the right place.

Example fix

// before
obs.Replay(scheduler); // obs is null
// after
var obs = obs ?? Observable.Empty<int>();
obs.Replay(scheduler);
Defensive patterns

Strategy: validation

Validate before calling

if (source == null) throw new InvalidOperationException("Replay(source, scheduler) requires a non-null source.");

Type guard

static bool CanReplayOn<T>(IObservable<T>? s, IScheduler sch) => s is not null && sch is not null;

Try / catch

try { var conn = source.Replay(scheduler); } catch (ArgumentNullException ex) when (ex.ParamName == "source") { /* handle */ }

Prevention

When it happens

Trigger: Calling Replay(null, scheduler), or chaining .Replay(scheduler) on a null IObservable reference.

Common situations: Constructing replay pipelines from configurable sources where a config-driven lookup returned null; test harnesses passing null sources.

Related errors


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

Appendix: source

Thrown at Rx.NET/Source/src/System.Reactive/Linq/Observable.Binding.cs:445

            return s_impl.Replay(source);
        }

        /// <summary>
        /// Returns a connectable observable sequence that shares a single subscription to the underlying sequence replaying all notifications.
        /// This operator is a specialization of Multicast using a <see cref="ReplaySubject{T}"/>.
        /// </summary>
        /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
        /// <param name="source">Source sequence whose elements will be multicasted through a single shared subscription.</param>
        /// <param name="scheduler">Scheduler where connected observers will be invoked on.</param>
        /// <returns>A connectable observable sequence that shares a single subscription to the underlying sequence.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="scheduler"/> is null.</exception>
        /// <remarks>Subscribers will receive all the notifications of the source.</remarks>
        /// <seealso cref="ReplaySubject{T}"/>
        public static IConnectableObservable<TSource> Replay<TSource>(this IObservable<TSource> source, IScheduler scheduler)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (scheduler == null)
            {
                throw new ArgumentNullException(nameof(scheduler));
            }

            return s_impl.Replay(source, scheduler);
        }

        /// <summary>
        /// Returns an observable sequence that is the result of invoking the selector on a connectable observable sequence that shares a single subscription to the underlying sequence replaying all notifications.
        /// This operator is a specialization of Multicast using a <see cref="ReplaySubject{T}"/>.
        /// </summary>
        /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
        /// <typeparam name="TResult">The type of the elements in the result sequence.</typeparam>
        /// <param name="source">Source sequence whose elements will be multicasted through a single shared subscription.</param>
        /// <param name="selector">Selector function which can use the multicasted source sequence as many times as needed, without causing multiple subscriptions to the source sequence. Subscribers to the given source will receive all the notifications of the source.</param>

View on GitHub (pinned to 94b5d5ab91)