dotnet/maui · critical · InvalidOperationException

Unable to find Application Services

Error message

Unable to find Application Services

What it means

MauiCompatInitializer.Initialize runs during MAUI's hosted-services init on Windows. It resolves IDispatcher from the DI container, falling back to IPlatformApplication.Current?.Services; if neither is available it throws InvalidOperationException('Unable to find Application Services'). This signals the DI container is missing core MAUI services.

Source

Thrown at src/Compatibility/Core/src/AppHostBuilderExtensions.cs:133

			return builder;
		}

		static MauiAppBuilder AddMauiCompat(this MauiAppBuilder builder)
		{
			builder.Services.TryAddEnumerable(ServiceDescriptor.Transient<IMauiInitializeService, MauiCompatInitializer>());
			return builder;
		}

		class MauiCompatInitializer : IMauiInitializeService
		{
			public void Initialize(IServiceProvider services)
			{
#if WINDOWS
				var dispatcher =
					services.GetService<IDispatcher>() ??
					IPlatformApplication.Current?.Services.GetRequiredService<IDispatcher>() ??
					throw new InvalidOperationException("Unable to find Application Services");

					dispatcher.DispatchIfRequired(() =>
					{
						var dictionaries = UI.Xaml.Application.Current?.Resources?.MergedDictionaries;
						if (UI.Xaml.Application.Current?.Resources != null && dictionaries != null)
						{
							// Microsoft.Maui.Controls.Compatibility
							UI.Xaml.Application.Current.Resources.AddLibraryResources("MicrosoftMauiControlsCompatibilityIncluded", "ms-appx:///Microsoft.Maui.Controls.Compatibility/Windows/Resources.xbf");
						}
					});
#endif
			}
		}
	}
}

View on GitHub (pinned to f377ff1c5e)

Solutions

  1. Build the application through the standard MAUI host (CreateMauiApp/UseMauiApp) so IDispatcher and IPlatformApplication are registered.
  2. If using a custom ServiceProvider, register IDispatcher and set IPlatformApplication.Current appropriately.
  3. Ensure UseMauiCompatibility() is applied to the builder so initializers run in the right order.

Example fix

// before — custom host missing services
var services = new ServiceCollection();
var sp = services.BuildServiceProvider();
new MauiCompatInitializer().Initialize(sp); // throws
// after — use the MAUI host
var app = MauiApp.CreateMauiApp().UseMauiApp<App>().UseMauiCompatibility();
Defensive patterns

Strategy: validation

Validate before calling

static IDispatcher ResolveDispatcher(IServiceProvider sp)
{
    var d = sp.GetService<IDispatcher>()
            ?? IPlatformApplication.Current?.Services?.GetService<IDispatcher>();
    if (d is null)
        throw new InvalidOperationException("Build the app via MauiApp.CreateMauiApp().UseMauiApp<T>() so IDispatcher is registered.");
    return d;
}

Type guard

static bool HostProvidesDispatcher(IServiceProvider sp)
    => sp.GetService<IDispatcher>() is not null
       || IPlatformApplication.Current?.Services?.GetService<IDispatcher>() is not null;

Prevention

When it happens

Trigger: IServiceProvider lacks IDispatcher AND IPlatformApplication.Current (or its Services) is null/unable to provide IDispatcher. Happens when the MAUI host was built without registering essential services or when the initializer runs in a context without the platform host.

Common situations: Calling compatibility APIs from a non-MAUI host (unit tests, a console host, design-time); a custom IServiceProvider not built via MauiApp/CreateMauiApp; DI registration order issue where MauiCompatInitializer runs before the dispatcher service is registered; partial setup where UseMauiApp<T>() was skipped.

Related errors


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