{"record":{"id":"9e229e1602859670","repo":"dotnet/maui","slug":"unable-to-find-the-required-services-please-add-a-9e229e","errorCode":null,"errorMessage":"Unable to find the required services. Please add all the required services by calling 'IServiceCollection.AddWindowsFormsBlazorWebView' in the application startup code.","messagePattern":"Unable to find the required services\\. Please add all the required services by calling 'IServiceCollection\\.AddWindowsFormsBlazorWebView' in the application startup code\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/BlazorWebView/src/SharedSource/WebView2WebViewManager.cs","lineNumber":107,"sourceCode":"\t\t\tIServiceProvider services,\n\t\t\tDispatcher dispatcher,\n\t\t\tIFileProvider fileProvider,\n\t\t\tJSComponentConfigurationStore jsComponents,\n\t\t\tstring contentRootRelativeToAppRoot,\n\t\t\tstring hostPagePathWithinFileProvider,\n\t\t\tAction<UrlLoadingEventArgs> urlLoading,\n\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>();","sourceCodeStart":89,"sourceCodeEnd":125,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/BlazorWebView/src/SharedSource/WebView2WebViewManager.cs#L89-L125","documentation":"The WebView2WebViewManager constructor, compiled under the WEBVIEW2_WINFORMS directive, checks that WindowsFormsBlazorMarkerService is registered. This marker is added exclusively by AddWindowsFormsBlazorWebView(). Without it, the Blazor rendering pipeline for WinForms was never bootstrapped, and the WebView2 control cannot host Razor components. This guard prevents cascading failures deeper in initialization.","triggerScenarios":"A WebView2WebViewManager is constructed (when a WinForms BlazorWebView control initializes its WebView2) with an IServiceProvider that lacks WindowsFormsBlazorMarkerService. This happens when the WinForms Program.cs or Form constructor does not call services.AddWindowsFormsBlazorWebView() before creating the BlazorWebView control.","commonSituations":"WinForms application that creates a BlazorWebView control without calling AddWindowsFormsBlazorWebView in the service collection setup; passing a newly constructed ServiceProvider that bypasses the registration; upgrading the BlazorWebView package where the registration method name or signature changed; DI container configuration in a separate method that was not called.","solutions":["Call services.AddWindowsFormsBlazorWebView() on your IServiceCollection before constructing the BlazorWebView control.","In Program.cs or Form1 constructor: var services = new ServiceCollection(); services.AddWindowsFormsBlazorWebView(); var sp = services.BuildServiceProvider(); then pass sp to the BlazorWebView.","Ensure the ServiceProvider passed to the BlazorWebView's Services property is the one that had AddWindowsFormsBlazorWebView called on it.","Verify you are not accidentally using AddWpfBlazorWebView or AddMauiBlazorWebView in a WinForms project."],"exampleFix":"// before\nvar services = new ServiceCollection();\nservices.AddBlazorWebView();\nvar sp = services.BuildServiceProvider();\n// after\nvar services = new ServiceCollection();\nservices.AddWindowsFormsBlazorWebView();\nvar sp = services.BuildServiceProvider();","handlingStrategy":"validation","validationCode":"// Verify marker service is registered before creating BlazorWebView\nvar services = new ServiceCollection();\nservices.AddWindowsFormsBlazorWebView();\nvar sp = services.BuildServiceProvider();\nif (sp.GetService<WindowsFormsBlazorMarkerService>() is null)\n{\n    throw new InvalidOperationException(\"AddWindowsFormsBlazorWebView was not called.\");\n}","typeGuard":null,"tryCatchPattern":"try\n{\n    // Code that creates/initializes a WinForms BlazorWebView\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"AddWindowsFormsBlazorWebView\"))\n{\n    logger.LogError(ex, \"WinForms Blazor services not registered. Call AddWindowsFormsBlazorWebView.\");\n    throw;\n}","preventionTips":["Call AddWindowsFormsBlazorWebView() as the first service registration in WinForms Program.cs.","Keep the ServiceProvider instance that was configured and pass it to the BlazorWebView.","Verify after every package upgrade that the registration method still exists and is called.","Add a smoke test that creates a BlazorWebView and verifies no marker exception."],"tags":["blazor","blazorwebview","winforms","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"}