dotnet/reactive · error · ArgumentNullException

nameof(observableFactory)

Error message

nameof(observableFactory)

What it means

Observable.Defer<TResult> takes a factory Func<IObservable<TResult>> invoked per subscription. Rx throws ArgumentNullException immediately when the factory is null because a null factory would make every subscription fail anyway; failing at composition gives a better stack trace.

Solutions

  1. Pass a non-null factory lambda, e.g. () => Observable.Return(value).
  2. Null-check the factory before calling Defer.
  3. Fix the factory provider/registry so it never yields null.

Example fix

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

Strategy: validation

Validate before calling

if (observableFactory is null) throw new ArgumentNullException(nameof(observableFactory));
var obs = Observable.Defer(observableFactory);

Type guard

bool IsValid<T>(Func<IObservable<T>> f) => f is not null;

Try / catch

try { var obs = Observable.Defer(observableFactory); }
catch (ArgumentNullException ex) when (ex.ParamName == "observableFactory") { /* fall back to Observable.Empty */ }

Prevention

When it happens

Trigger: Calling Observable.Defer<TResult>(observableFactory) with a null Func<IObservable<TResult>> argument.

Common situations: A nullable Func field assigned later, a configuration-driven factory lookup returning null, or caching logic that stores null when the real factory failed to build.

Related errors


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

Appendix: source

Thrown at Rx.NET/Source/src/System.Reactive/Linq/Observable.Creation.cs:192

            return s_impl.Create(subscribeAsync);
        }

        #endregion

        #region + Defer +

        /// <summary>
        /// Returns an observable sequence that invokes the specified factory function whenever a new observer subscribes.
        /// </summary>
        /// <typeparam name="TResult">The type of the elements in the sequence returned by the factory function, and in the resulting sequence.</typeparam>
        /// <param name="observableFactory">Observable factory function to invoke for each observer that subscribes to the resulting sequence.</param>
        /// <returns>An observable sequence whose observers trigger an invocation of the given observable factory function.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="observableFactory"/> is null.</exception>
        public static IObservable<TResult> Defer<TResult>(Func<IObservable<TResult>> observableFactory)
        {
            if (observableFactory == null)
            {
                throw new ArgumentNullException(nameof(observableFactory));
            }

            return s_impl.Defer(observableFactory);
        }

        #endregion

        #region + DeferAsync +

        /// <summary>
        /// Returns an observable sequence that starts the specified asynchronous factory function whenever a new observer subscribes.
        /// </summary>
        /// <typeparam name="TResult">The type of the elements in the sequence returned by the factory function, and in the resulting sequence.</typeparam>
        /// <param name="observableFactoryAsync">Asynchronous factory function to start for each observer that subscribes to the resulting sequence.</param>
        /// <returns>An observable sequence whose observers trigger the given asynchronous observable factory function to be started.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="observableFactoryAsync"/> is null.</exception>
        /// <remarks>This operator is especially useful in conjunction with the asynchronous programming features introduced in C# 5.0 and Visual Basic 11.</remarks>
        public static IObservable<TResult> Defer<TResult>(Func<Task<IObservable<TResult>>> observableFactoryAsync)

View on GitHub (pinned to 94b5d5ab91)