dotnet/reactive · error · ArgumentNullException
Value cannot be null. (Parameter 'resourceFactoryAsync')
Error message
Value cannot be null. (Parameter 'resourceFactoryAsync')
What it means
The async overload Observable.Using<TResult,TResource>(Func<CancellationToken,Task<TResource>>, ...) needs a resourceFactoryAsync delegate to create the IDisposable resource asynchronously. The API eagerly throws ArgumentNullException when it is null.
Solutions
- Pass a valid async factory, e.g. async ct => await CreateResourceAsync(ct)
- Ensure methods returning Func<CancellationToken,Task<TResource>> are not returning null
- Initialize the delegate before invoking Observable.Using
Example fix
// before Observable.Using<int, DbConnection>(null, (conn, ct) => QueryAsync(conn, ct)); // after Observable.Using(ct => OpenConnectionAsync(ct), (conn, ct) => QueryAsync(conn, ct));
Defensive patterns
Strategy: validation
Validate before calling
if (resourceFactoryAsync == null) throw new ArgumentNullException(nameof(resourceFactoryAsync)); if (observableFactoryAsync == null) throw new ArgumentNullException(nameof(observableFactoryAsync)); var seq = Observable.Using(resourceFactoryAsync, observableFactoryAsync);
Type guard
static bool HasAsyncResourceFactory<TResource>(Func<CancellationToken, Task<TResource>> f) where TResource : IDisposable => f is not null;
Try / catch
try
{
var seq = Observable.Using(resourceFactoryAsync, observableFactoryAsync);
}
catch (ArgumentNullException ex) when (ex.ParamName == "resourceFactoryAsync")
{
throw new InvalidOperationException("An async resource factory is required", ex);
} Prevention
- Pass async lambdas directly (ct => CreateAsync(ct)) rather than through nullable intermediate variables
- Keep sync and async Using call sites clearly separated to avoid delegate-slot mix-ups
- Enable nullable reference types to surface unassigned Func fields
When it happens
Trigger: Calling the Task-based Observable.Using overload with a null resourceFactoryAsync (Func<CancellationToken, Task<TResource>>) as the first argument.
Common situations: Async factory methods assigned conditionally; awaiting configuration that resolves a null delegate; wrappers built over the async overload that pass through nulls from callers.
Related errors
- Value cannot be null. (Parameter 'end')
- end
- begin
- Value cannot be null. (Parameter 'functionAsync')
- Value cannot be null. (Parameter 'options')
AI-assisted analysis of dotnet/reactive@94b5d5ab91 (2026-09-15).
Data as JSON: /api/errors/0adbf808bf6914b9.
Report an issue: GitHub.
Appendix: source
Thrown at Rx.NET/Source/src/System.Reactive/Linq/Observable.Creation.cs:735
#region + UsingAsync +
/// <summary>
/// Constructs an observable sequence that depends on a resource object, whose lifetime is tied to the resulting observable sequence's lifetime. The resource is obtained and used through asynchronous methods.
/// The CancellationToken passed to the asynchronous methods is tied to the returned disposable subscription, allowing best-effort cancellation at any stage of the resource acquisition or usage.
/// </summary>
/// <typeparam name="TResult">The type of the elements in the produced sequence.</typeparam>
/// <typeparam name="TResource">The type of the resource used during the generation of the resulting sequence. Needs to implement <see cref="IDisposable"/>.</typeparam>
/// <param name="resourceFactoryAsync">Asynchronous factory function to obtain a resource object.</param>
/// <param name="observableFactoryAsync">Asynchronous factory function to obtain an observable sequence that depends on the obtained resource.</param>
/// <returns>An observable sequence whose lifetime controls the lifetime of the dependent resource object.</returns>
/// <exception cref="ArgumentNullException"><paramref name="resourceFactoryAsync"/> or <paramref name="observableFactoryAsync"/> 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 resource factory and observable factory functions will be signaled.</remarks>
public static IObservable<TResult> Using<TResult, TResource>(Func<CancellationToken, Task<TResource>> resourceFactoryAsync, Func<TResource, CancellationToken, Task<IObservable<TResult>>> observableFactoryAsync) where TResource : IDisposable
{
if (resourceFactoryAsync == null)
{
throw new ArgumentNullException(nameof(resourceFactoryAsync));
}
if (observableFactoryAsync == null)
{
throw new ArgumentNullException(nameof(observableFactoryAsync));
}
return s_impl.Using(resourceFactoryAsync, observableFactoryAsync);
}
#endregion
}
}
View on GitHub (pinned to 94b5d5ab91)