dotnet/reactive · error · ArgumentNullException

Value cannot be null. (Parameter 'source')

Error message

Value cannot be null. (Parameter 'source')

What it means

AsyncObservable.Aggregate validates its inputs and throws ArgumentNullException when source is null ('Value cannot be null. (Parameter source)'). The aggregate operator needs a real observable to reduce over; a null source cannot produce a seed or result. The guard runs synchronously before the operator pipeline is constructed.

Solutions

  1. Ensure the source is created before aggregation, e.g. via AsyncObservable.Create or another operator.
  2. Check the factory/method that produced the source for null-returning paths.
  3. Add a null check or assert at the point of source construction to surface the origin earlier.

Example fix

// before
var sum = await AsyncObservable.Aggregate(source, (acc, x) => acc + x); // source was null
// after
if (source == null) throw new InvalidOperationException("source not initialized");
var sum = await AsyncObservable.Aggregate(source, (acc, x) => acc + x);
Defensive patterns

Strategy: validation

Validate before calling

if (source is null) throw new InvalidOperationException("source must be non-null for Aggregate");
var result = await AsyncObservable.Aggregate(source, func);

Type guard

bool IsValidSource<T>(IAsyncObservable<T> s) => s is not null;

Try / catch

try
{
    var result = await AsyncObservable.Aggregate(source, func);
}
catch (ArgumentNullException ex) when (ex.ParamName == "source")
{
    // source construction failed upstream; log and provide fallback/default observable
}

Prevention

When it happens

Trigger: Calling AsyncObservable.Aggregate(null, func) — e.g. the observable came from an operator/factory that returned null, or an unassigned IAsyncObservable<T> field was passed.

Common situations: Conditional query construction where a branch skipped creating the source; DI or configuration returning null for an observable dependency; passing the result of an expression like GetSource() that can return null.

Related errors


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

Appendix: source

Thrown at AsyncRx.NET/System.Reactive.Async/Linq/Operators/Aggregate.cs:14

// 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. 

using System.Threading.Tasks;

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

            return CreateAsyncObservable<TSource>.From(
                source,
                func,
                static (source, func, observer) => source.SubscribeSafeAsync(AsyncObserver.Aggregate(observer, func)));
        }

        public static IAsyncObservable<TSource> Aggregate<TSource>(this IAsyncObservable<TSource> source, Func<TSource, TSource, ValueTask<TSource>> func)
        {
            if (source == null)
                throw new ArgumentNullException(nameof(source));
            if (func == null)
                throw new ArgumentNullException(nameof(func));

            return CreateAsyncObservable<TSource>.From(
                source,

View on GitHub (pinned to 94b5d5ab91)