dotnet/maui · critical · InvalidOperationException

Unable to find Application Services

Error message

Unable to find Application Services

What it means

AppHostBuilderExtensions.Windows hooks the Windows OnLaunching callback to bootstrap compatibility Forms.Init. It resolves `IPlatformApplication.Current?.Services`; if Current is null or Services is null (the .NET MAUI application host was never built/started), it throws InvalidOperationException('Unable to find Application Services').

Source

Thrown at src/Compatibility/Core/src/AppHostBuilderExtensions.Windows.cs:36

	public static partial class AppHostBuilderExtensions
	{
		internal static MauiAppBuilder ConfigureCompatibilityLifecycleEvents(this MauiAppBuilder builder) =>
			builder.ConfigureLifecycleEvents(events => events.AddWindows(OnConfigureLifeCycle));

		static void OnConfigureLifeCycle(IWindowsLifecycleBuilder windows)
		{
			windows.OnLaunching((app, args) =>
			{
				// This is the initial Init to set up any system services registered by
				// Forms.Init(). This happens before any UI has appeared.
				// This creates a dummy MauiContext.
				// We need to call this so the Window and Root Page can new up successfully
				// The dispatcher that's inside of Forms.Init needs to be setup before the initial
				// window and root page start creating.
				// Inside OnLaunched we grab the MauiContext that's on the window so we can have the correct
				// MauiContext inside Forms

				var services = IPlatformApplication.Current?.Services ?? throw new InvalidOperationException("Unable to find Application Services");
				var mauiContext = new MauiContext(services);
				var state = new ActivationState(mauiContext, args);
#pragma warning disable CS0612 // Type or member is obsolete
				Forms.Init(state, new InitializationOptions { Flags = InitializationFlags.SkipRenderers });
#pragma warning restore CS0612 // Type or member is obsolete
			})
			.OnMauiContextCreated((mauiContext) =>
			{
				// This is the final Init that sets up the real context from the application.

				var state = new ActivationState(mauiContext);
#pragma warning disable CS0612 // Type or member is obsolete
				Forms.Init(state);
#pragma warning restore CS0612 // Type or member is obsolete
			});
		}
	}
}

View on GitHub (pinned to f377ff1c5e)

Solutions

  1. Ensure the app entry point builds the MAUI application: MauiApp.CreateMauiApp().UseMauiApp<TApp>().UseMauiCompatibility().
  2. Confirm IPlatformApplication.Current is set by the host before compatibility code runs (it normally is when using the MAUI bootstrapper).
  3. If running outside a hosted MAUI app (tests, design-time), set up a minimal service provider that provides IDispatcher and required services.

Example fix

// before — Windows app without MAUI host
var mauiContext = new MauiContext(IPlatformApplication.Current?.Services); // Current is null
// after — build the MAUI host first
var builder = MauiApp.CreateMauiApp()
    .UseMauiApp<App>()
    .UseMauiCompatibility();
Defensive patterns

Strategy: validation

Validate before calling

static IServiceProvider EnsureServices()
{
    var services = IPlatformApplication.Current?.Services
        ?? throw new InvalidOperationException("MAUI host not built — call UseMauiApp<T>() before compatibility APIs.");
    return services;
}

Type guard

static bool HasMauiHost => IPlatformApplication.Current?.Services is not null;

Prevention

When it happens

Trigger: The compatibility bootstrap runs before the MAUI application has initialized its IPlatformApplication — e.g. the WinUI app's OnLaunched fires but MauiProgram/CreateMauiApp was not invoked, or IPlatformApplication.Current was not set.

Common situations: A WinUI 3 / Windows App SDK project missing the `mauiAppBuilder.UseMauiApp<TApp>()` / `.UseMauiCompatibility()` wiring; calling compatibility APIs from a context where the MAUI host was not created (unit test, design-time, a plain WinUI app without the MAUI host); version mismatch between the app and Microsoft.Maui.Controls.Compatibility.

Related errors


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