dotnet/reactive · error · ArgumentNullException

scheduler

Error message

scheduler

What it means

Interval(period, scheduler) throws ArgumentNullException with paramName "scheduler" when the IAsyncScheduler argument is null. A scheduler is mandatory in this overload because Interval relies on it to schedule each tick.

Solutions

  1. Pass a valid IAsyncScheduler instance (e.g. the default scheduler exposed by the library).
  2. Fix DI registration/initialization so the scheduler is resolved before Interval is called.
  3. Null-check the scheduler at your composition root and fail with a clear message.

Example fix

// before
var xs = AsyncObservable.Interval(period, _testScheduler); // still null in this test
// after
Assert.NotNull(_testScheduler);
var xs = AsyncObservable.Interval(period, _testScheduler ?? new DefaultAsyncScheduler());
Defensive patterns

Strategy: validation

Validate before calling

if (scheduler is null) throw new ArgumentException("scheduler must be non-null", nameof(scheduler));
// or: scheduler ??= new DefaultAsyncScheduler();

Type guard

static bool HasScheduler(IAsyncScheduler s) => s is not null;

Try / catch

try { var xs = AsyncObservable.Interval(period, scheduler); }
catch (ArgumentNullException ex) when (ex.ParamName == "scheduler") { /* resolve/initialize scheduler */ }

Prevention

When it happens

Trigger: Calling AsyncObservable.Interval(period, null), typically when the scheduler comes from an optional dependency, DI resolution failure, or a TestScheduler field that was never initialized.

Common situations: Unit tests where the TestScheduler is set up lazily and is still null, or DI containers that register the scheduler conditionally.

Related errors


AI-assisted analysis of dotnet/reactive@94b5d5ab91 (2026-09-15). Data as JSON: /api/errors/af8263a3e0932c56. Report an issue: GitHub.

Appendix: source

Thrown at AsyncRx.NET/System.Reactive.Async/Linq/Operators/Interval.cs:25

namespace System.Reactive.Linq
{
    public partial class AsyncObservable
    {
        public static IAsyncObservable<long> Interval(TimeSpan period)
        {
            if (period < TimeSpan.Zero)
                throw new ArgumentOutOfRangeException(nameof(period));

            return Create<long>(observer => AsyncObserver.Interval(observer, period));
        }

        public static IAsyncObservable<long> Interval(TimeSpan period, IAsyncScheduler scheduler)
        {
            if (period < TimeSpan.Zero)
                throw new ArgumentOutOfRangeException(nameof(period));
            if (scheduler == null)
                throw new ArgumentNullException(nameof(scheduler));

            return Create<long>(observer => AsyncObserver.Interval(observer, period, scheduler));
        }
    }

    public partial class AsyncObserver
    {
        public static ValueTask<IAsyncDisposable> Interval(IAsyncObserver<long> observer, TimeSpan period) => Timer(observer, period, period);

        public static ValueTask<IAsyncDisposable> Interval(IAsyncObserver<long> observer, TimeSpan period, IAsyncScheduler scheduler) => Timer(observer, period, period, scheduler);
    }
}

View on GitHub (pinned to 94b5d5ab91)