dotnet/reactive · error · ArgumentNullException

ArgumentNullException: source

Error message

ArgumentNullException: source

What it means

The ToAsyncAction extension converts an IObservable<TSource> into a WinRT IAsyncAction; a null source throws ArgumentNullException('source'). The library checks before wrapping the sequence via AsyncInfo.Run so the failure is immediate rather than when the async action starts.

Solutions

  1. Ensure the observable is non-null before calling ToAsyncAction.
  2. If the source may be absent, use Observable.Empty<TSource>() or Observable.Throw instead of null.
  3. Add a null check or coalesce at the call site.

Example fix

// before
myObservable.ToAsyncAction(); // myObservable == null
// after
var src = myObservable ?? Observable.Empty<Unit>();
src.ToAsyncAction();
Defensive patterns

Strategy: validation

Validate before calling

if (source is null) throw new InvalidOperationException("source observable not initialized");
var action = (source ?? Observable.Empty<TSource>()).ToAsyncAction();

Type guard

bool IsConvertible<TSource>(IObservable<TSource>? s) => s is not null;

Try / catch

try { var a = source.ToAsyncAction(); }
catch (ArgumentNullException ex) when (ex.ParamName == "source") { /* obtain a valid observable */ }

Prevention

When it happens

Trigger: Calling observable.ToAsyncAction() where observable is null, e.g. a method returning IObservable that returned null or an uninitialized field.

Common situations: Factory methods that return null on error instead of Observable.Throw, binding results from DI or configuration that were never populated, null result of a conditional expression.

Related errors


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

Appendix: source

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

    /// </summary>
    [CLSCompliant(false)]
    public static class AsyncInfoObservable
    {
        #region IAsyncAction

        /// <summary>
        /// Creates a Windows Runtime asynchronous action that represents the completion of the observable sequence.
        /// Upon cancellation of the asynchronous action, the subscription to the source sequence will be disposed.
        /// </summary>
        /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
        /// <param name="source">Source sequence to expose as an asynchronous action.</param>
        /// <returns>Windows Runtime asynchronous action object representing the completion of the observable sequence.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
        public static IAsyncAction ToAsyncAction<TSource>(this IObservable<TSource> source)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            return AsyncInfo.Run(ct => (Task)source.DefaultIfEmpty().ToTask(ct));
        }

        #region Progress

        /// <summary>
        /// Creates a Windows Runtime asynchronous action that represents the completion of the observable sequence, reporting incremental progress for each element produced by the sequence.
        /// Upon cancellation of the asynchronous action, the subscription to the source sequence will be disposed.
        /// </summary>
        /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
        /// <param name="source">Source sequence to expose as an asynchronous action.</param>
        /// <returns>Windows Runtime asynchronous action object representing the completion of the observable sequence, reporting incremental progress for each source sequence element.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
        public static IAsyncActionWithProgress<int> ToAsyncActionWithProgress<TSource>(this IObservable<TSource> source)
        {
            if (source == null)

View on GitHub (pinned to 94b5d5ab91)