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

  1. Pass a valid async factory, e.g. async ct => await CreateResourceAsync(ct)
  2. Ensure methods returning Func<CancellationToken,Task<TResource>> are not returning null
  3. 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

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


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)