dotnet/reactive · error · ArgumentNullException
Argument cannot be null (Parameter name: source)
Error message
Argument cannot be null (Parameter name: source)
What it means
FirstOrDefault<TSource>(IAsyncObservable<TSource>) (the no-predicate overload) throws ArgumentNullException when source is null. AsyncRx's operator API is extension-method based; a null source would cause a NullReferenceException only at Subscribe time, so the library guards eagerly and names the offending parameter. This keeps the stack trace at your call site.
Solutions
- Fix the upstream factory/lookup so it returns a non-null IAsyncObservable; throw your own descriptive exception if the source cannot be resolved.
- Verify you are calling the operator on the observable itself (source.FirstOrDefaultAsync()) and not passing a null into a static helper.
- Guard before the call: if (source == null) throw new InvalidOperationException(...).
Example fix
// before
var result = sources[name].FirstOrDefaultAsync(); // sources[name] may be null
// after
if (!sources.TryGetValue(name, out var src)) throw new KeyNotFoundException($"source '{name}' missing");
var result = src.FirstOrDefaultAsync(); Defensive patterns
Strategy: validation
Validate before calling
if (source is null) throw new InvalidOperationException("source observable not resolved");
var first = source.FirstOrDefaultAsync(); Type guard
static bool HasSource<T>(IAsyncObservable<T>? s) => s is not null;
Try / catch
try { var first = src.FirstOrDefaultAsync(); }
catch (ArgumentNullException ex) when (ex.ParamName == "source") { /* log and use fallback stream */ } Prevention
- Check every operator earlier in the chain for null-returning branches
- Validate DI registrations resolve to non-null observables at startup
- Prefer throwing descriptive exceptions at resolution time over null propagation
When it happens
Trigger: Calling source.FirstOrDefault() / FirstOrDefaultAsync(source) where the IAsyncObservable<TSource> variable is null — e.g. a factory or dictionary lookup returned null before the extension call.
Common situations: Chained operator pipelines where an earlier operator unexpectedly returned null; config-driven source selection where the configured source key resolves to nothing; caching of observables that was never populated.
Related errors
- Argument cannot be null (Parameter name: predicate)
- Argument cannot be null (Parameter name: observer)
- Value cannot be null. (Parameter 'onErrorAsync')
- Value cannot be null. (Parameter 'onCompletedAsync')
- Value cannot be null. (Parameter 'error')
AI-assisted analysis of dotnet/reactive@94b5d5ab91 (2026-09-15).
Data as JSON: /api/errors/c8f5e8b90d492796.
Report an issue: GitHub.
Appendix: source
Thrown at AsyncRx.NET/System.Reactive.Async/Linq/Operators/FirstOrDefault.cs:18
// 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> FirstOrDefaultAsync<TSource>(this IAsyncObservable<TSource> source) => FirstOrDefault(source);
public static IAsyncObservable<TSource> FirstOrDefaultAsync<TSource>(this IAsyncObservable<TSource> source, Func<TSource, bool> predicate) => FirstOrDefault(source, predicate);
public static IAsyncObservable<TSource> FirstOrDefaultAsync<TSource>(this IAsyncObservable<TSource> source, Func<TSource, ValueTask<bool>> predicate) => FirstOrDefault(source, predicate);
public static IAsyncObservable<TSource> FirstOrDefault<TSource>(this IAsyncObservable<TSource> source)
{
if (source == null)
throw new ArgumentNullException(nameof(source));
return Create(source, static (source, observer) => source.SubscribeSafeAsync(AsyncObserver.FirstOrDefault(observer)));
}
public static IAsyncObservable<TSource> FirstOrDefault<TSource>(this IAsyncObservable<TSource> source, Func<TSource, bool> predicate)
{
if (source == null)
throw new ArgumentNullException(nameof(source));
if (predicate == null)
throw new ArgumentNullException(nameof(predicate));
return Create(
source,
predicate,
static (source, predicate, observer) => source.SubscribeSafeAsync(AsyncObserver.FirstOrDefault(observer, predicate)));
}
public static IAsyncObservable<TSource> FirstOrDefault<TSource>(this IAsyncObservable<TSource> source, Func<TSource, ValueTask<bool>> predicate)View on GitHub (pinned to 94b5d5ab91)