dotnet/maui · error · InvalidOperationException

Unable to find the required services. Please add all the req

Error message

Unable to find the required services. Please add all the required services by calling 'IServiceCollection.AddWindowsFormsBlazorWebView' in the application startup code.

What it means

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.

Source

Thrown at src/BlazorWebView/src/SharedSource/WebView2WebViewManager.cs:107

			IServiceProvider services,
			Dispatcher dispatcher,
			IFileProvider fileProvider,
			JSComponentConfigurationStore jsComponents,
			string contentRootRelativeToAppRoot,
			string hostPagePathWithinFileProvider,
			Action<UrlLoadingEventArgs> urlLoading,
			Action<BlazorWebViewInitializingEventArgs> blazorWebViewInitializing,
			Action<BlazorWebViewInitializedEventArgs> blazorWebViewInitialized,
			ILogger logger)
			: base(services, dispatcher, AppOriginUri, fileProvider, jsComponents, hostPagePathWithinFileProvider)

		{
			ArgumentNullException.ThrowIfNull(webview);

#if WEBVIEW2_WINFORMS
			if (services.GetService<WindowsFormsBlazorMarkerService>() is null)
			{
				throw new InvalidOperationException(
					"Unable to find the required services. " +
					$"Please add all the required services by calling '{nameof(IServiceCollection)}.{nameof(BlazorWebViewServiceCollectionExtensions.AddWindowsFormsBlazorWebView)}' in the application startup code.");
			}
#elif WEBVIEW2_WPF
			if (services.GetService<WpfBlazorMarkerService>() is null)
			{
				throw new InvalidOperationException(
					"Unable to find the required services. " +
					$"Please add all the required services by calling '{nameof(IServiceCollection)}.{nameof(BlazorWebViewServiceCollectionExtensions.AddWpfBlazorWebView)}' in the application startup code.");
			}
#endif

			_logger = logger;
			_webview = webview;
			_urlLoading = urlLoading;
			_blazorWebViewInitializing = blazorWebViewInitializing;
			_blazorWebViewInitialized = blazorWebViewInitialized;
			_developerTools = services.GetRequiredService<BlazorWebViewDeveloperTools>();

View on GitHub (pinned to f377ff1c5e)

Solutions

  1. Call services.AddWindowsFormsBlazorWebView() on your IServiceCollection before constructing the BlazorWebView control.
  2. In Program.cs or Form1 constructor: var services = new ServiceCollection(); services.AddWindowsFormsBlazorWebView(); var sp = services.BuildServiceProvider(); then pass sp to the BlazorWebView.
  3. Ensure the ServiceProvider passed to the BlazorWebView's Services property is the one that had AddWindowsFormsBlazorWebView called on it.
  4. Verify you are not accidentally using AddWpfBlazorWebView or AddMauiBlazorWebView in a WinForms project.

Example fix

// before
var services = new ServiceCollection();
services.AddBlazorWebView();
var sp = services.BuildServiceProvider();
// after
var services = new ServiceCollection();
services.AddWindowsFormsBlazorWebView();
var sp = services.BuildServiceProvider();
Defensive patterns

Strategy: validation

Validate before calling

// Verify marker service is registered before creating BlazorWebView
var services = new ServiceCollection();
services.AddWindowsFormsBlazorWebView();
var sp = services.BuildServiceProvider();
if (sp.GetService<WindowsFormsBlazorMarkerService>() is null)
{
    throw new InvalidOperationException("AddWindowsFormsBlazorWebView was not called.");
}

Try / catch

try
{
    // Code that creates/initializes a WinForms BlazorWebView
}
catch (InvalidOperationException ex) when (ex.Message.Contains("AddWindowsFormsBlazorWebView"))
{
    logger.LogError(ex, "WinForms Blazor services not registered. Call AddWindowsFormsBlazorWebView.");
    throw;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of dotnet/maui@f377ff1c5e (2026-08-13). Data as JSON: /api/errors/9e229e1602859670. Report an issue: GitHub.