dotnet/reactive · error · ArgumentNullException
Value cannot be null. (Parameter 'observableFactory')
Error message
Value cannot be null. (Parameter 'observableFactory')
What it means
AsyncObservable.Defer requires a non-null observableFactory Func that produces the IAsyncObservable when subscribed. The library validates it eagerly and throws ArgumentNullException (Parameter 'observableFactory') because deferring a null factory would fail at subscribe time with no useful stack.
Solutions
- Pass a valid factory lambda, e.g. () => AsyncObservable.Return(42).
- Check that the variable holding the factory is initialized before calling Defer.
- If the factory comes from configuration/DI, validate it resolved successfully before deferring.
Example fix
// before Func<IAsyncObservable<int>> factory = null; var xs = AsyncObservable.Defer(factory); // after var xs = AsyncObservable.Defer(() => AsyncObservable.Return(42));
Defensive patterns
Strategy: validation
Validate before calling
if (observableFactory is null) throw new ArgumentNullException(nameof(observableFactory)); var xs = AsyncObservable.Defer(observableFactory);
Type guard
static bool IsValidFactory<T>(Func<IAsyncObservable<T>> f) => f is not null;
Try / catch
try
{
var xs = AsyncObservable.Defer(factory);
}
catch (ArgumentNullException ex) when (ex.ParamName == "observableFactory")
{
// factory delegate was null; ensure it is assigned before deferring
} Prevention
- Assign factory delegates at construction time, not lazily.
- Prefer inline lambdas over nullable delegate fields when the source is static.
- If factories come from config/DI, fail fast at startup when resolution returns null.
When it happens
Trigger: Calling AsyncObservable.Defer<TSource>((Func<IAsyncObservable<TSource>>)null); also reachable via DeferAsync or FromAsync, which delegate to Defer, when their factory arguments are null.
Common situations: Passing a method group that resolves to null via reflection or conditional delegation; a config-selected factory string that failed to resolve; overloads where Func vs Func<ValueTask<...>> ambiguity led to casting null.
Related errors
- observableFactory
- Value cannot be null. (Parameter 'observer')
- Value cannot be null. (Parameter 'source')
- Value cannot be null. (Parameter 'scheduler')
- observer
AI-assisted analysis of dotnet/reactive@94b5d5ab91 (2026-09-15).
Data as JSON: /api/errors/53c79bc5c413cf00.
Report an issue: GitHub.
Appendix: source
Thrown at AsyncRx.NET/System.Reactive.Async/Linq/Operators/Defer.cs:16
// 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.Reactive.Disposables;
using System.Threading;
using System.Threading.Tasks;
namespace System.Reactive.Linq
{
public partial class AsyncObservable
{
public static IAsyncObservable<TSource> Defer<TSource>(Func<IAsyncObservable<TSource>> observableFactory)
{
if (observableFactory == null)
throw new ArgumentNullException(nameof(observableFactory));
return Defer(() => new ValueTask<IAsyncObservable<TSource>>(observableFactory()));
}
public static IAsyncObservable<TSource> DeferAsync<TSource>(Func<ValueTask<IAsyncObservable<TSource>>> observableFactory) => Defer(observableFactory);
public static IAsyncObservable<TSource> Defer<TSource>(Func<ValueTask<IAsyncObservable<TSource>>> observableFactory)
{
if (observableFactory == null)
throw new ArgumentNullException(nameof(observableFactory));
return Create<TSource>(async observer =>
{
var source = default(IAsyncObservable<TSource>);
try
{
source = await observableFactory().ConfigureAwait(false);View on GitHub (pinned to 94b5d5ab91)