dotnet/reactive · error · ArgumentNullException

dependencyObject (Parameter 'dependencyObject')

Error message

dependencyObject (Parameter 'dependencyObject')

What it means

To derive the dispatcher, this overload reads dependencyObject.Dispatcher; a null DependencyObject makes that impossible, so ArgumentNullException with ParamName 'dependencyObject' is thrown. Validation is eager at call time, before CoreDispatcherScheduler is created.

Solutions

  1. Pass a real DependencyObject such as the page or control; verify the element exists in the visual tree first.
  2. Resolve the element after Loaded/template-applied events rather than in the constructor.
  3. If you only have a dispatcher, switch to the ObserveOn(CoreDispatcher) overload instead.

Example fix

// before
var el = (FrameworkElement)tree.FindName("missing");
stream.ObserveOn(el); // throws when el is null
// after
var el = (FrameworkElement)tree.FindName("target");
if (el != null) stream.ObserveOn(el).Subscribe(...);
Defensive patterns

Strategy: validation

Validate before calling

var target = container.FindName("myControl") as DependencyObject;
if (target == null) return; // element not loaded yet; defer subscription

Type guard

bool HasDependencyObject(DependencyObject o) => o is not null;

Try / catch

try { stream.ObserveOn(dependencyObject).Subscribe(onNext); }
catch (ArgumentNullException ex) when (ex.ParamName == "dependencyObject") { /* defer to Loaded event and retry */ }

Prevention

When it happens

Trigger: Calling source.ObserveOn(null) with the DependencyObject overload, or passing an object whose resolution failed (e.g. FindName returned null, binding produced no target).

Common situations: FindName/VisualTreeHelper lookups returning null for elements not yet in the visual tree; passing a control from a template before it is applied; DI-resolved page/view being null in tests.

Related errors


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

Appendix: source

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

#if IS_UAP
        /// <summary>
        /// Wraps the source sequence in order to run its observer callbacks 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>
        /// <returns>The source sequence whose observations happen on the specified object's dispatcher.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="dependencyObject"/> is null.</exception>
        public static IObservable<TSource> ObserveOn<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.ObserveOn(source, new CoreDispatcherScheduler(dependencyObject.Dispatcher));
        }

        /// <summary>
        /// Wraps the source sequence in order to run its observer callbacks 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 observations happen on the specified object's dispatcher.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="dependencyObject"/> is null.</exception>
        public static IObservable<TSource> ObserveOn<TSource>(this IObservable<TSource> source, DependencyObject dependencyObject, CoreDispatcherPriority priority)
        {
            if (source == null)
            {

View on GitHub (pinned to 94b5d5ab91)