{"record":{"id":"75e440824c600b9e","repo":"dotnet/maui","slug":"unable-to-find-the-required-services-please-add-a-75e440","errorCode":null,"errorMessage":"Unable to find the required services. Please add all the required services by calling 'IServiceCollection.AddWpfBlazorWebView' in the application startup code.","messagePattern":"Unable to find the required services\\. Please add all the required services by calling 'IServiceCollection\\.AddWpfBlazorWebView' in the application startup code\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/BlazorWebView/src/SharedSource/WebView2WebViewManager.cs","lineNumber":114,"sourceCode":"\t\t\tAction<BlazorWebViewInitializingEventArgs> blazorWebViewInitializing,\n\t\t\tAction<BlazorWebViewInitializedEventArgs> blazorWebViewInitialized,\n\t\t\tILogger logger)\n\t\t\t: base(services, dispatcher, AppOriginUri, fileProvider, jsComponents, hostPagePathWithinFileProvider)\n\n\t\t{\n\t\t\tArgumentNullException.ThrowIfNull(webview);\n\n#if WEBVIEW2_WINFORMS\n\t\t\tif (services.GetService<WindowsFormsBlazorMarkerService>() 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.AddWindowsFormsBlazorWebView)}' in the application startup code.\");\n\t\t\t}\n#elif WEBVIEW2_WPF\n\t\t\tif (services.GetService<WpfBlazorMarkerService>() 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.AddWpfBlazorWebView)}' in the application startup code.\");\n\t\t\t}\n#endif\n\n\t\t\t_logger = logger;\n\t\t\t_webview = webview;\n\t\t\t_urlLoading = urlLoading;\n\t\t\t_blazorWebViewInitializing = blazorWebViewInitializing;\n\t\t\t_blazorWebViewInitialized = blazorWebViewInitialized;\n\t\t\t_developerTools = services.GetRequiredService<BlazorWebViewDeveloperTools>();\n\t\t\t_contentRootRelativeToAppRoot = contentRootRelativeToAppRoot;\n\n\t\t\t// Unfortunately the CoreWebView2 can only be instantiated asynchronously.\n\t\t\t// We want the external API to behave as if initalization is synchronous,\n\t\t\t// so keep track of a task we can await during LoadUri.\n\t\t\t_webviewReadyTask = TryInitializeWebView2();\n\t\t}","sourceCodeStart":96,"sourceCodeEnd":132,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/BlazorWebView/src/SharedSource/WebView2WebViewManager.cs#L96-L132","documentation":"The WebView2WebViewManager constructor, compiled under the WEBVIEW2_WPF directive (#elif), checks that WpfBlazorMarkerService is registered. This marker is added exclusively by AddWpfBlazorWebView(). Without it, the Blazor services for WPF (component rendering, JS interop, dispatcher marshalling) were never set up. The check is the WPF counterpart of the WinForms marker check.","triggerScenarios":"A WebView2WebViewManager is constructed when a WPF BlazorWebView control initializes, but the IServiceProvider lacks WpfBlazorMarkerService. This occurs when App.xaml.cs or the window's constructor does not call AddWpfBlazorWebView() on the service collection before the BlazorWebView is created.","commonSituations":"WPF application that creates a BlazorWebView in XAML or code without calling AddWpfBlazorWebView in the service configuration; using AddWindowsFormsBlazorWebView by mistake in a WPF project; ServiceProvider created before the registration call; package upgrade where the method was renamed or the directive changed.","solutions":["Call services.AddWpfBlazorWebView() on your IServiceCollection in App.xaml.cs or the window constructor before creating the BlazorWebView.","Ensure the ServiceProvider passed to the BlazorWebView Services property is the built result of the collection that had AddWpfBlazorWebView called.","Double-check you are calling the WPF variant (AddWpfBlazorWebView), not the WinForms or MAUI variant.","If using a DI container like Autofac or Lamar, ensure the marker service singleton is registered through the adapter."],"exampleFix":"// before\nvar services = new ServiceCollection();\nservices.AddWindowsFormsBlazorWebView(); // wrong platform!\nvar sp = services.BuildServiceProvider();\n// after\nvar services = new ServiceCollection();\nservices.AddWpfBlazorWebView();\nvar sp = services.BuildServiceProvider();","handlingStrategy":"validation","validationCode":"// Verify marker service is registered before creating BlazorWebView\nvar services = new ServiceCollection();\nservices.AddWpfBlazorWebView();\nvar sp = services.BuildServiceProvider();\nif (sp.GetService<WpfBlazorMarkerService>() is null)\n{\n    throw new InvalidOperationException(\"AddWpfBlazorWebView was not called.\");\n}","typeGuard":null,"tryCatchPattern":"try\n{\n    // Code that creates/initializes a WPF BlazorWebView\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"AddWpfBlazorWebView\"))\n{\n    logger.LogError(ex, \"WPF Blazor services not registered. Call AddWpfBlazorWebView.\");\n    throw;\n}","preventionTips":["Call AddWpfBlazorWebView() in App.xaml.cs before any BlazorWebView is created.","Double-check you are using the WPF variant, not WinForms or MAUI.","Pass the configured ServiceProvider to the BlazorWebView Services property.","Add a smoke test that verifies WpfBlazorMarkerService is registered."],"tags":["blazor","blazorwebview","wpf","dependency-injection","service-registration","webview2"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}