dotnet/reactive · error · ArgumentNullException

Value cannot be null. (Parameter 'source')

Error message

Value cannot be null. (Parameter 'source')

What it means

AsAsyncObservable wraps an IAsyncObservable<TSource> to guarantee a stable async-observable identity, and it throws ArgumentNullException when the source is null. The library validates arguments eagerly at the call site so subscription-time failures are avoided. There is no meaningful wrapper for a null source.

Solutions

  1. Ensure the receiver is a real observable before calling, e.g. source ?? AsyncObservable.Empty<T>() or another default instance.
  2. Fix the upstream factory/lookup that produced the null source.
  3. Add a null check with a clear exception at the point where the source is created, so the failure is reported with context.
  4. If the value is legitimately optional, branch on null instead of calling the operator unconditionally.

Example fix

// before
var wrapped = maybeSource.AsAsyncObservable(); // maybeSource is null

// after
if (maybeSource == null) throw new InvalidOperationException("source was not initialized");
var wrapped = maybeSource.AsAsyncObservable();
Defensive patterns

Strategy: validation

Validate before calling

if (source == null)
    source = AsyncObservable.Empty<T>();

Type guard

static bool IsObservable<T>(IAsyncObservable<T>? o) => o is not null;

Try / catch

try
{
    var wrapped = maybeSource.AsAsyncObservable();
}
catch (ArgumentNullException ex) when (ex.ParamName == "source")
{
    var wrapped = AsyncObservable.Empty<T>();
}

Prevention

When it happens

Trigger: Calling source.AsAsyncObservable() where the extension-method receiver is null — e.g. a method returning IAsyncObservable<T> returned null, a dictionary/lookup miss yielded null, or an optional dependency was not configured.

Common situations: Chaining AsAsyncObservable onto the result of a factory that returned null; calling it on a field populated asynchronously that has not been initialized yet; interop code where a null sync observable was converted to a null async one.

Related errors


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

Appendix: source

Thrown at AsyncRx.NET/System.Reactive.Async/Linq/Operators/AsAsyncObservable.cs:12

// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT License.
// See the LICENSE file in the project root for more information. 

namespace System.Reactive.Linq
{
    public partial class AsyncObservable
    {
        public static IAsyncObservable<TSource> AsAsyncObservable<TSource>(this IAsyncObservable<TSource> source)
        {
            if (source == null)
                throw new ArgumentNullException(nameof(source));

            return Create(source, static (source, observer) => source.SubscribeSafeAsync(observer));
        }
    }
}

View on GitHub (pinned to 94b5d5ab91)