dotnet/reactive · error · ArgumentNullException
nameof(subscribeAsync)
Error message
nameof(subscribeAsync)
What it means
Observable.Create<TResult> with an async subscribe delegate (Func<IObserver<TResult>, CancellationToken, Task>) throws ArgumentNullException when subscribeAsync is null. This overload supports async/await-based observables, wiring a CancellationToken that fires on disposal. Rx validates the delegate immediately at Create.
Solutions
- Pass a valid async lambda: async (o, ct) => { ... }
- Fix the delegate source that returned null
- Guard the delegate for null before Create
Example fix
// before
var obs = Observable.Create<int>(subscribeAsync); // subscribeAsync is null
// after
var obs = Observable.Create<int>(async (o, ct) =>
{
await Task.Yield();
o.OnNext(1);
o.OnCompleted();
}); Defensive patterns
Strategy: validation
Validate before calling
if (subscribeAsync == null) throw new ArgumentNullException(nameof(subscribeAsync)); var obs = Observable.Create<T>(subscribeAsync);
Type guard
bool IsValid<T>(Func<IObserver<T>, CancellationToken, Task> f) => f is not null;
Try / catch
try { var obs = Observable.Create<T>(subscribeAsync); }
catch (ArgumentNullException ex) when (ex.ParamName == "subscribeAsync") { /* handle null delegate */ } Prevention
- Pass async lambdas directly to Create rather than storing them in nullable fields
- Keep async subscribe methods non-static-safe: verify receiver instance is non-null before method-group reference
- Compile with nullable reference types so unassigned delegates are flagged
When it happens
Trigger: Calling Observable.Create<TResult>(subscribeAsync) with a null Func<IObserver<TResult>, CancellationToken, Task>, e.g. an unassigned async method reference or a factory returning null.
Common situations: Async observable construction where the async lambda was refactored into a method that no longer exists, or DI/config-driven delegate selection yielding null.
Related errors
AI-assisted analysis of dotnet/reactive@94b5d5ab91 (2026-09-15).
Data as JSON: /api/errors/05fb2e24cab7b4cc.
Report an issue: GitHub.
Appendix: source
Thrown at Rx.NET/Source/src/System.Reactive/Linq/Observable.Creation.cs:77
#endregion
#region + CreateAsync +
/// <summary>
/// Creates an observable sequence from a specified cancellable asynchronous Subscribe method.
/// The CancellationToken passed to the asynchronous Subscribe method is tied to the returned disposable subscription, allowing best-effort cancellation.
/// </summary>
/// <typeparam name="TResult">The type of the elements in the produced sequence.</typeparam>
/// <param name="subscribeAsync">Asynchronous method used to produce elements.</param>
/// <returns>The observable sequence surfacing the elements produced by the asynchronous method.</returns>
/// <exception cref="ArgumentNullException"><paramref name="subscribeAsync"/> is null.</exception>
/// <remarks>This operator is especially useful in conjunction with the asynchronous programming features introduced in C# 5.0 and Visual Basic 11.</remarks>
/// <remarks>When a subscription to the resulting sequence is disposed, the CancellationToken that was fed to the asynchronous subscribe function will be signaled.</remarks>
public static IObservable<TResult> Create<TResult>(Func<IObserver<TResult>, CancellationToken, Task> subscribeAsync)
{
if (subscribeAsync == null)
{
throw new ArgumentNullException(nameof(subscribeAsync));
}
return s_impl.Create(subscribeAsync);
}
/// <summary>
/// Creates an observable sequence from a specified asynchronous Subscribe method.
/// </summary>
/// <typeparam name="TResult">The type of the elements in the produced sequence.</typeparam>
/// <param name="subscribeAsync">Asynchronous method used to produce elements.</param>
/// <returns>The observable sequence surfacing the elements produced by the asynchronous method.</returns>
/// <exception cref="ArgumentNullException"><paramref name="subscribeAsync"/> is null.</exception>
/// <remarks>This operator is especially useful in conjunction with the asynchronous programming features introduced in C# 5.0 and Visual Basic 11.</remarks>
public static IObservable<TResult> Create<TResult>(Func<IObserver<TResult>, Task> subscribeAsync)
{
if (subscribeAsync == null)
{
throw new ArgumentNullException(nameof(subscribeAsync));View on GitHub (pinned to 94b5d5ab91)