dotnet/maui · error · InvalidOperationException

The dispatcher was not found and the current application doe

Error message

The dispatcher was not found and the current application does not have a dispatcher.

What it means

EnsureDispatcher throws InvalidOperationException when the passed dispatcher is null and neither Dispatcher.GetForCurrentThread() nor Application.Current?.Dispatcher yields a dispatcher. This is the guard used by dispatching extension methods to fail fast rather than silently drop a dispatched action.

Source

Thrown at src/Controls/src/Core/DispatcherExtensions.cs:104

				return action();
			}
		}

		static IDispatcher EnsureDispatcher(IDispatcher? dispatcher)
		{
			if (dispatcher is not null)
				return dispatcher;

			// maybe this thread has a dispatcher
			if (Dispatcher.GetForCurrentThread() is IDispatcher globalDispatcher)
				return globalDispatcher;

			// try looking on the app
			if (Application.Current?.Dispatcher is IDispatcher appDispatcher)
				return appDispatcher;

			// no dispatchers found at all
			throw new InvalidOperationException("The dispatcher was not found and the current application does not have a dispatcher.");
		}
	}
}

View on GitHub (pinned to f377ff1c5e)

Solutions

  1. Capture the dispatcher on the UI thread and pass it explicitly to dispatching calls.
  2. Guard with Dispatcher.GetForCurrentThread() before invoking, and skip work if null in non-UI contexts.
  3. Initialize Application.Current (and thus its dispatcher) before off-thread dispatching.

Example fix

// before
IDispatcher d = null;
d.DispatchIfRequired(action); // throws, no dispatcher

// after
var d = Dispatcher.GetForCurrentThread();
if (d is not null) d.DispatchIfRequired(action);
else { /* queue or run on main thread */ }
Defensive patterns

Strategy: validation

Validate before calling

static bool HasAnyDispatcher(IDispatcher? d) =>
    d is not null
    || Dispatcher.GetForCurrentThread() is not null
    || (Application.Current?.Dispatcher is not null);

Prevention

When it happens

Trigger: Calling Dispatch/DispatchIfRequired/etc. with a null dispatcher from a non-UI thread while Application.Current is null or its dispatcher is unset; using Controls in a host with no dispatcher infrastructure.

Common situations: Background-thread access to dispatching APIs in test hosts or services; calling extensions before the application dispatcher is created.

Related errors


AI-assisted analysis of dotnet/maui@f377ff1c5e (2026-08-13). Data as JSON: /api/errors/87b433d96208af63. Report an issue: GitHub.