dotnet/reactive · error · ArgumentNullException

selector

Error message

selector

What it means

The SelectMany overload that maps each element to an IAsyncOperation (WinRT) throws ArgumentNullException when the selector delegate is null. Rx validates arguments eagerly at subscription composition time so failures surface at the call site rather than inside the pipeline. A null selector means the library has no way to produce a per-element asynchronous operation.

Solutions

  1. Ensure a non-null selector delegate is passed to SelectMany before calling it.
  2. If the selector is optional, guard with `if (selector != null) source.SelectMany(selector)` or use a default selector.
  3. Check DI configuration / factory registration so the delegate actually resolves to an instance.

Example fix

// before
source.SelectMany(_selector.ToObservable()); // _selector is null
// after
if (_selector == null) throw new InvalidOperationException("selector not configured");
source.SelectMany(x => _selector(x).ToObservable());
Defensive patterns

Strategy: validation

Validate before calling

if (source == null) throw new ArgumentNullException(nameof(source));
if (selector == null) throw new ArgumentNullException(nameof(selector));

Type guard

bool IsValidSelector<TSource, TResult>(Func<TSource, IAsyncOperation<TResult>> s) => s != null;

Try / catch

try { return source.SelectMany(x => selector(x).ToObservable()); }
catch (ArgumentNullException ex) when (ex.ParamName == "selector") { /* substitute default selector or rethrow with context */ throw; }

Prevention

When it happens

Trigger: Calling source.SelectMany(x => someAsyncOp) where the lambda variable or a captured delegate field holding the selector is null, e.g. selector passed in as a parameter and forwarded without a null check.

Common situations: Conditional composition where a selector is only assigned on one code path; DI-injected converter delegates that failed to register; refactoring that left a null default for an optional selector parameter.

Related errors


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

Appendix: source

Thrown at Rx.NET/Source/src/System.Reactive/Platforms/WinRT/Linq/WindowsObservable.StandardSequenceOperators.cs:32

        /// Projects each element of an observable sequence to a Windows Runtime asynchronous operation and merges all of the asynchronous operation results into one observable sequence.
        /// </summary>
        /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
        /// <typeparam name="TResult">The type of the result produced by the projected asynchronous operations and the elements in the merged result sequence.</typeparam>
        /// <param name="source">An observable sequence of elements to project.</param>
        /// <param name="selector">A transform function to apply to each element.</param>
        /// <returns>An observable sequence whose elements are the result of the asynchronous operations executed for each element of the input sequence.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
        /// <remarks>This overload supports composition of observable sequences and Windows Runtime asynchronous operations, without requiring manual conversion of the asynchronous operations to observable sequences using <see cref="AsyncInfoObservableExtensions.ToObservable{TResult}(IAsyncOperation{TResult})"/>.</remarks>
        public static IObservable<TResult> SelectMany<TSource, TResult>(this IObservable<TSource> source, Func<TSource, IAsyncOperation<TResult>> selector)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

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

            return source.SelectMany(x => selector(x).ToObservable());
        }

        /// <summary>
        /// Projects each element of an observable sequence to a Windows Runtime asynchronous operation and merges all of the asynchronous operation results into one observable sequence.
        /// </summary>
        /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
        /// <typeparam name="TResult">The type of the result produced by the projected asynchronous operations and the elements in the merged result sequence.</typeparam>
        /// <typeparam name="TProgress">The type of the reported progress objects, which get ignored by this query operator.</typeparam>
        /// <param name="source">An observable sequence of elements to project.</param>
        /// <param name="selector">A transform function to apply to each element.</param>
        /// <returns>An observable sequence whose elements are the result of the asynchronous operations executed for each element of the input sequence.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
        /// <remarks>This overload supports composition of observable sequences and Windows Runtime asynchronous operations, without requiring manual conversion of the asynchronous operations to observable sequences using <see cref="AsyncInfoObservableExtensions.ToObservable{TResult}(IAsyncOperation{TResult})"/>.</remarks>
        public static IObservable<TResult> SelectMany<TSource, TResult, TProgress>(this IObservable<TSource> source, Func<TSource, IAsyncOperationWithProgress<TResult, TProgress>> selector)
        {

View on GitHub (pinned to 94b5d5ab91)