dotnet/reactive · error · ArgumentNullException

dependencyObject

Error message

dependencyObject

What it means

SubscribeOn(source, dependencyObject) throws ArgumentNullException when the dependencyObject parameter is null. The method needs it solely to read dependencyObject.Dispatcher and construct a CoreDispatcherScheduler. A null element therefore cannot provide a subscription target, and the library fails fast at composition time.

Solutions

  1. Defer the call until the element is loaded (hook Loaded/Frame.Loaded and build the pipeline there).
  2. Pass a CoreDispatcher captured earlier instead of a DependencyObject: SubscribeOn(source, dispatcher).
  3. Null-check the element and fall back to CoreDispatcherScheduler.Current.Dispatcher captured on the UI thread.
  4. If the element legitimately may be absent, skip applying SubscribeOn rather than passing null.

Example fix

// before
var target = (Panel)this.FindName("ResultsPanel"); // null if name missing
stream.SubscribeOn(target).Subscribe(...);
// after
var target = (Panel)this.FindName("ResultsPanel");
if (target != null) stream.SubscribeOn(target).Subscribe(...);
else stream.SubscribeOn(App.UiDispatcher).Subscribe(...);
Defensive patterns

Strategy: validation

Validate before calling

var target = this.FindName("ResultsPanel") as DependencyObject;
if (target != null) source = source.SubscribeOn(target);
else source = source.SubscribeOn(App.UiDispatcher);

Type guard

bool HasValidTarget(DependencyObject o) => o != null && o.Dispatcher != null;

Try / catch

try { source.SubscribeOn(dependencyObject).Subscribe(handler); }
catch (ArgumentNullException ex) when (ex.ParamName == "dependencyObject")
{ source.SubscribeOn(App.UiDispatcher).Subscribe(handler); }

Prevention

When it happens

Trigger: Passing a null DependencyObject — e.g. a control resolved via FindName that does not exist in the visual tree yet, a Content binding that is null at pipeline-construction time, or a page/element reference taken after unloading.

Common situations: Building pipelines in a control's constructor before the visual tree is loaded; OnNavigatedFrom cleanup running after the element was released; ItemTemplate elements whose DataContext resolves to null during virtualization.

Related errors


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

Appendix: source

Thrown at Rx.NET/Source/src/System.Reactive/Platforms/WinRT/Linq/CoreDispatcherObservable.cs:234

        /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
        /// <param name="source">Source sequence.</param>
        /// <param name="dependencyObject">Object to get the dispatcher from.</param>
        /// <returns>The source sequence whose subscriptions and unsubscriptions happen on the specified object's dispatcher.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="dependencyObject"/> is null.</exception>
        /// <remarks>
        /// Only the side-effects of subscribing to the source sequence and disposing subscriptions to the source sequence are run on the dispatcher associated with the specified object.
        /// In order to invoke observer callbacks on the dispatcher associated with the specified object, e.g. to render results in a control, use <see cref="CoreDispatcherObservable.ObserveOn{TSource}(IObservable{TSource}, DependencyObject)"/>.
        /// </remarks>
        public static IObservable<TSource> SubscribeOn<TSource>(this IObservable<TSource> source, DependencyObject dependencyObject)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (dependencyObject == null)
            {
                throw new ArgumentNullException(nameof(dependencyObject));
            }

            return Synchronization.SubscribeOn(source, new CoreDispatcherScheduler(dependencyObject.Dispatcher));
        }

        /// <summary>
        /// Wraps the source sequence in order to run its subscription and unsubscription logic on the dispatcher associated with the specified object.
        /// </summary>
        /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
        /// <param name="source">Source sequence.</param>
        /// <param name="dependencyObject">Object to get the dispatcher from.</param>
        /// <param name="priority">Priority to schedule work items at.</param>
        /// <returns>The source sequence whose subscriptions and unsubscriptions happen on the specified object's dispatcher.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="dependencyObject"/> is null.</exception>
        /// <remarks>
        /// Only the side-effects of subscribing to the source sequence and disposing subscriptions to the source sequence are run on the dispatcher associated with the specified object.
        /// In order to invoke observer callbacks on the dispatcher associated with the specified object, e.g. to render results in a control, use <see cref="CoreDispatcherObservable.ObserveOn{TSource}(IObservable{TSource}, DependencyObject, CoreDispatcherPriority)"/>.
        /// </remarks>

View on GitHub (pinned to 94b5d5ab91)