dotnet/reactive · error · ArgumentNullException

ArgumentNullException: resultSelector

Error message

ArgumentNullException: resultSelector

What it means

ToAsyncOperationWithProgress also requires a non-null resultSelector — the function that maps the source observable to the result observable — and throws ArgumentNullException for 'resultSelector' when it is null. The selector defines the whole computation, so a null value would make the async operation non-functional.

Solutions

  1. Pass a valid Func<IObservable<TSource>, IObservable<TResult>> as the second argument.
  2. If the selector is computed, initialize it (e.g. xs => xs.LastAsync()) before invoking the method.
  3. Guard with a null check and throw a descriptive error in your own code before calling.

Example fix

// before
Func<IObservable<int>, IObservable<int>> selector = null;
var op = source.ToAsyncOperationWithProgress(selector);
// after
var op = source.ToAsyncOperationWithProgress(xs => xs.LastAsync());
Defensive patterns

Strategy: validation

Validate before calling

Func<IObservable<TSrc>, IObservable<TRes>> selector = xs => xs.LastAsync();
if (selector is null) throw new InvalidOperationException("Selector not configured");
var op = source.ToAsyncOperationWithProgress(selector);

Type guard

static bool IsValidSelector<TSrc, TRes>(Func<IObservable<TSrc>, IObservable<TRes>> f) => f is not null;

Try / catch

try
{
    var op = source.ToAsyncOperationWithProgress(selector);
}
catch (ArgumentNullException ex) when (ex.ParamName == "resultSelector")
{
    log.Warn("resultSelector was null; check DI registration/argument order.");
}

Prevention

When it happens

Trigger: Calling source.ToAsyncOperationWithProgress(null), often when the selector is stored in a nullable Func field/variable that was never assigned, or when building the delegate conditionally.

Common situations: Dependency-injected selector strategies that defaulted to null, MVVM scenarios where the query factory is bound late, or typos passing the wrong variable (e.g. the progress selector) as the result selector.

Related errors


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

Appendix: source

Thrown at Rx.NET/Source/src/System.Reactive.WindowsRuntime/System.Reactive.Linq/AsyncInfoObservable.cs:175

        /// Creates a Windows Runtime asynchronous operation that returns the last element of the result sequence, reporting incremental progress for each element produced by the source sequence.
        /// Upon cancellation of the asynchronous operation, the subscription to the source sequence will be disposed.
        /// </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 to compute a result sequence that gets exposed as an asynchronous operation.</param>
        /// <param name="resultSelector">Selector function to map the source sequence on a result sequence.</param>
        /// <returns>Windows Runtime asynchronous operation object that returns the last element of the result sequence, reporting incremental progress for each source sequence element.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="resultSelector"/> is null.</exception>
        public static IAsyncOperationWithProgress<TResult, int> ToAsyncOperationWithProgress<TSource, TResult>(this IObservable<TSource> source, Func<IObservable<TSource>, IObservable<TResult>> resultSelector)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

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

            return AsyncInfo.Run<TResult, int>((ct, progress) =>
            {
                var i = 0;
                return resultSelector(source.Do(_ => progress.Report(i++))).ToTask(ct);
            });
        }

        /// <summary>
        /// Creates a Windows Runtime asynchronous operation that returns the last element of the result sequence, using a selector function to map the source sequence on a progress reporting sequence.
        /// Upon cancellation of the asynchronous operation, the subscription to the source sequence will be disposed.
        /// </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>
        /// <typeparam name="TProgress">The type of the elements in the progress sequence.</typeparam>
        /// <param name="source">Source sequence to compute a result sequence that gets exposed as an asynchronous operation and a progress sequence that gets reported through the asynchronous operation.</param>
        /// <param name="resultSelector">Selector function to map the source sequence on a result sequence.</param>

View on GitHub (pinned to 94b5d5ab91)