{"record":{"id":"d290c9b7ed6cf5ee","repo":"dotnet/maui","slug":"unable-to-find-the-required-services-please-add-a","errorCode":null,"errorMessage":"Unable to find the required services. Please add all the required services by calling 'IServiceCollection.AddMauiBlazorWebView' in the application startup code.","messagePattern":"Unable to find the required services\\. Please add all the required services by calling 'IServiceCollection\\.AddMauiBlazorWebView' in the application startup code\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/BlazorWebView/src/Maui/Android/AndroidWebKitWebViewManager.cs","lineNumber":50,"sourceCode":"\t\t/// <summary>\n\t\t/// Constructs an instance of <see cref=\"AndroidWebKitWebViewManager\"/>.\n\t\t/// </summary>\n\t\t/// <param name=\"webview\">A wrapper to access platform-specific WebView APIs.</param>\n\t\t/// <param name=\"services\">A service provider containing services to be used by this class and also by application code.</param>\n\t\t/// <param name=\"dispatcher\">A <see cref=\"Dispatcher\"/> instance that can marshal calls to the required thread or sync context.</param>\n\t\t/// <param name=\"fileProvider\">Provides static content to the webview.</param>\n\t\t/// <param name=\"contentRootRelativeToAppRoot\">Path to the directory containing application content files.</param>\n\t\t/// <param name=\"hostPageRelativePath\">Path to the host page within the <paramref name=\"fileProvider\"/>.</param>\n\t\t/// <param name=\"logger\">Logger to send log messages to.</param>\n\t\tpublic AndroidWebKitWebViewManager(AWebView webview, IServiceProvider services, Dispatcher dispatcher, IFileProvider fileProvider, JSComponentConfigurationStore jsComponents, string contentRootRelativeToAppRoot, string hostPageRelativePath, ILogger logger)\n\t\t\t: base(services, dispatcher, AppOriginUri, fileProvider, jsComponents, hostPageRelativePath)\n\t\t{\n\t\t\tArgumentNullException.ThrowIfNull(webview);\n\n#if WEBVIEW2_MAUI\n\t\t\tif (services.GetService<MauiBlazorMarkerService>() is null)\n\t\t\t{\n\t\t\t\tthrow new InvalidOperationException(\n\t\t\t\t\t\"Unable to find the required services. \" +\n\t\t\t\t\t$\"Please add all the required services by calling '{nameof(IServiceCollection)}.{nameof(BlazorWebViewServiceCollectionExtensions.AddMauiBlazorWebView)}' in the application startup code.\");\n\t\t\t}\n#endif\n\t\t\t_logger = logger;\n\n\t\t\t_webview = webview;\n\t\t\t_contentRootRelativeToAppRoot = contentRootRelativeToAppRoot;\n\t\t}\n\n\t\t/// <inheritdoc />\n\t\tprotected override void NavigateCore(Uri absoluteUri)\n\t\t{\n\t\t\t_logger.NavigatingToUri(absoluteUri);\n\t\t\t_webview.LoadUrl(absoluteUri.AbsoluteUri);\n\t\t}\n\n\t\t/// <inheritdoc />","sourceCodeStart":32,"sourceCodeEnd":68,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/BlazorWebView/src/Maui/Android/AndroidWebKitWebViewManager.cs#L32-L68","documentation":"The AndroidWebKitWebViewManager constructor checks that MauiBlazorMarkerService is registered in the DI container. This marker service is only added when you call AddMauiBlazorWebView() on the IServiceCollection. The check (wrapped in #if WEBVIEW2_MAUI) exists so developers get a clear message instead of a cryptic null-reference later in initialization. If the marker is absent, the Blazor runtime services (component discovery, JS interop, etc.) were never wired up.","triggerScenarios":"Constructing an AndroidWebKitWebViewManager (which happens when the BlazorWebViewHandler creates its platform webview manager on Android) with an IServiceProvider that lacks MauiBlazorMarkerService. This occurs when MauiProgram.CreateMauiApp() does not chain .UseMauiApp(...).ConfigureServices(s => s.AddMauiBlazorWebView()) or when the BlazorWebView is instantiated before the handler pipeline runs.","commonSituations":"Upgrading from a .NET version where AddMauiBlazorWebView was called implicitly to one where it must be explicit; adding a BlazorWebView to a page but forgetting to register services in MauiProgram.cs; using a custom MauiAppBuilder that skips the standard UseMauiApp call; DI container scoping issue where the handler receives a scoped provider that does not contain the singleton marker.","solutions":["Call builder.UseMauiApp<App>().ConfigureServices(services => services.AddMauiBlazorWebView()) in your MauiProgram.CreateMauiApp method.","If using the fluent API, ensure AddMauiBlazorWebView() is called before the app is built: builder.Services.AddMauiBlazorWebView().","Verify no custom handler registration overrides BlazorWebViewHandler without preserving the service registration step.","Check that you are not passing a manually constructed IServiceProvider to the handler instead of the one from the MAUI host."],"exampleFix":"// before\npublic static MauiApp CreateMauiApp(MauiAppBuilder builder)\n{\n    builder.UseMauiApp<App>();\n    return builder.Build();\n}\n// after\npublic static MauiApp CreateMauiApp(MauiAppBuilder builder)\n{\n    builder.UseMauiApp<App>()\n           .Services.AddMauiBlazorWebView();\n    return builder.Build();\n}","handlingStrategy":"validation","validationCode":"// Verify marker service is registered before using BlazorWebView\nvar services = new ServiceCollection();\nservices.AddMauiBlazorWebView();\nvar sp = services.BuildServiceProvider();\nif (sp.GetService<MauiBlazorMarkerService>() is null)\n{\n    throw new InvalidOperationException(\"AddMauiBlazorWebView was not called.\");\n}","typeGuard":null,"tryCatchPattern":"try\n{\n    // code that constructs AndroidWebKitWebViewManager\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"AddMauiBlazorWebView\"))\n{\n    // Log the actionable message and ensure AddMauiBlazorWebView is called in MauiProgram\n    logger.LogError(ex, \"Blazor services not registered. Call AddMauiBlazorWebView in MauiProgram.\");\n    throw;\n}","preventionTips":["Always call AddMauiBlazorWebView() in MauiProgram.CreateMauiApp as a standard step when adding BlazorWebView to a MAUI app.","Add a unit test that verifies MauiBlazorMarkerService is registered after CreateMauiApp.","Keep Microsoft.AspNetCore.Components.WebView.Maui and Microsoft.Maui.Controls package versions aligned.","Review MauiProgram.cs after any package upgrade."],"tags":["blazor","blazorwebview","maui","android","dependency-injection","service-registration"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}