{"record":{"id":"9852c49dc0146230","repo":"dotnet/maui","slug":"unable-to-find-application-services","errorCode":null,"errorMessage":"Unable to find Application Services","messagePattern":"Unable to find Application Services","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"critical","filePath":"src/Compatibility/Core/src/AppHostBuilderExtensions.Windows.cs","lineNumber":36,"sourceCode":"\tpublic static partial class AppHostBuilderExtensions\n\t{\n\t\tinternal static MauiAppBuilder ConfigureCompatibilityLifecycleEvents(this MauiAppBuilder builder) =>\n\t\t\tbuilder.ConfigureLifecycleEvents(events => events.AddWindows(OnConfigureLifeCycle));\n\n\t\tstatic void OnConfigureLifeCycle(IWindowsLifecycleBuilder windows)\n\t\t{\n\t\t\twindows.OnLaunching((app, args) =>\n\t\t\t{\n\t\t\t\t// This is the initial Init to set up any system services registered by\n\t\t\t\t// Forms.Init(). This happens before any UI has appeared.\n\t\t\t\t// This creates a dummy MauiContext.\n\t\t\t\t// We need to call this so the Window and Root Page can new up successfully\n\t\t\t\t// The dispatcher that's inside of Forms.Init needs to be setup before the initial\n\t\t\t\t// window and root page start creating.\n\t\t\t\t// Inside OnLaunched we grab the MauiContext that's on the window so we can have the correct\n\t\t\t\t// MauiContext inside Forms\n\n\t\t\t\tvar services = IPlatformApplication.Current?.Services ?? throw new InvalidOperationException(\"Unable to find Application Services\");\n\t\t\t\tvar mauiContext = new MauiContext(services);\n\t\t\t\tvar state = new ActivationState(mauiContext, args);\n#pragma warning disable CS0612 // Type or member is obsolete\n\t\t\t\tForms.Init(state, new InitializationOptions { Flags = InitializationFlags.SkipRenderers });\n#pragma warning restore CS0612 // Type or member is obsolete\n\t\t\t})\n\t\t\t.OnMauiContextCreated((mauiContext) =>\n\t\t\t{\n\t\t\t\t// This is the final Init that sets up the real context from the application.\n\n\t\t\t\tvar state = new ActivationState(mauiContext);\n#pragma warning disable CS0612 // Type or member is obsolete\n\t\t\t\tForms.Init(state);\n#pragma warning restore CS0612 // Type or member is obsolete\n\t\t\t});\n\t\t}\n\t}\n}","sourceCodeStart":18,"sourceCodeEnd":54,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Compatibility/Core/src/AppHostBuilderExtensions.Windows.cs#L18-L54","documentation":"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').","triggerScenarios":"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.","commonSituations":"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.","solutions":["Ensure the app entry point builds the MAUI application: MauiApp.CreateMauiApp().UseMauiApp<TApp>().UseMauiCompatibility().","Confirm IPlatformApplication.Current is set by the host before compatibility code runs (it normally is when using the MAUI bootstrapper).","If running outside a hosted MAUI app (tests, design-time), set up a minimal service provider that provides IDispatcher and required services."],"exampleFix":"// before — Windows app without MAUI host\nvar mauiContext = new MauiContext(IPlatformApplication.Current?.Services); // Current is null\n// after — build the MAUI host first\nvar builder = MauiApp.CreateMauiApp()\n    .UseMauiApp<App>()\n    .UseMauiCompatibility();","handlingStrategy":"validation","validationCode":"static IServiceProvider EnsureServices()\n{\n    var services = IPlatformApplication.Current?.Services\n        ?? throw new InvalidOperationException(\"MAUI host not built — call UseMauiApp<T>() before compatibility APIs.\");\n    return services;\n}","typeGuard":"static bool HasMauiHost => IPlatformApplication.Current?.Services is not null;","tryCatchPattern":null,"preventionTips":["Build the MAUI host in the app entry (CreateMauiApp/UseMauiApp) before any compatibility API.","Verify IPlatformApplication.Current is non-null in startup tests.","Don't run compatibility code in non-hosted contexts without a stub host."],"tags":["windows","maui-host","di","startup","maui-compat","platform"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}