{"record":{"id":"fc0d7e52484bfe6f","repo":"dotnet/maui","slug":"unable-to-find-application-services-fc0d7e","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.cs","lineNumber":133,"sourceCode":"\n\t\t\treturn builder;\n\t\t}\n\n\t\tstatic MauiAppBuilder AddMauiCompat(this MauiAppBuilder builder)\n\t\t{\n\t\t\tbuilder.Services.TryAddEnumerable(ServiceDescriptor.Transient<IMauiInitializeService, MauiCompatInitializer>());\n\t\t\treturn builder;\n\t\t}\n\n\t\tclass MauiCompatInitializer : IMauiInitializeService\n\t\t{\n\t\t\tpublic void Initialize(IServiceProvider services)\n\t\t\t{\n#if WINDOWS\n\t\t\t\tvar dispatcher =\n\t\t\t\t\tservices.GetService<IDispatcher>() ??\n\t\t\t\t\tIPlatformApplication.Current?.Services.GetRequiredService<IDispatcher>() ??\n\t\t\t\t\tthrow new InvalidOperationException(\"Unable to find Application Services\");\n\n\t\t\t\t\tdispatcher.DispatchIfRequired(() =>\n\t\t\t\t\t{\n\t\t\t\t\t\tvar dictionaries = UI.Xaml.Application.Current?.Resources?.MergedDictionaries;\n\t\t\t\t\t\tif (UI.Xaml.Application.Current?.Resources != null && dictionaries != null)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t// Microsoft.Maui.Controls.Compatibility\n\t\t\t\t\t\t\tUI.Xaml.Application.Current.Resources.AddLibraryResources(\"MicrosoftMauiControlsCompatibilityIncluded\", \"ms-appx:///Microsoft.Maui.Controls.Compatibility/Windows/Resources.xbf\");\n\t\t\t\t\t\t}\n\t\t\t\t\t});\n#endif\n\t\t\t}\n\t\t}\n\t}\n}\n","sourceCodeStart":115,"sourceCodeEnd":149,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Compatibility/Core/src/AppHostBuilderExtensions.cs#L115-L149","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Build the application through the standard MAUI host (CreateMauiApp/UseMauiApp) so IDispatcher and IPlatformApplication are registered.","If using a custom ServiceProvider, register IDispatcher and set IPlatformApplication.Current appropriately.","Ensure UseMauiCompatibility() is applied to the builder so initializers run in the right order."],"exampleFix":"// before — custom host missing services\nvar services = new ServiceCollection();\nvar sp = services.BuildServiceProvider();\nnew MauiCompatInitializer().Initialize(sp); // throws\n// after — use the MAUI host\nvar app = MauiApp.CreateMauiApp().UseMauiApp<App>().UseMauiCompatibility();","handlingStrategy":"validation","validationCode":"static IDispatcher ResolveDispatcher(IServiceProvider sp)\n{\n    var d = sp.GetService<IDispatcher>()\n            ?? IPlatformApplication.Current?.Services?.GetService<IDispatcher>();\n    if (d is null)\n        throw new InvalidOperationException(\"Build the app via MauiApp.CreateMauiApp().UseMauiApp<T>() so IDispatcher is registered.\");\n    return d;\n}","typeGuard":"static bool HostProvidesDispatcher(IServiceProvider sp)\n    => sp.GetService<IDispatcher>() is not null\n       || IPlatformApplication.Current?.Services?.GetService<IDispatcher>() is not null;","tryCatchPattern":null,"preventionTips":["Always build through the MAUI host so IDispatcher and platform services register.","For custom/test hosts, register IDispatcher and set IPlatformApplication.Current.","Apply UseMauiCompatibility() to the builder."],"tags":["windows","maui-host","di","dispatcher","startup","maui-compat"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}