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.AddMauiBlazorWebView' in the application startup code.

What it means

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.

Source

Thrown at src/BlazorWebView/src/Maui/Android/AndroidWebKitWebViewManager.cs:50

		/// <summary>
		/// Constructs an instance of <see cref="AndroidWebKitWebViewManager"/>.
		/// </summary>
		/// <param name="webview">A wrapper to access platform-specific WebView APIs.</param>
		/// <param name="services">A service provider containing services to be used by this class and also by application code.</param>
		/// <param name="dispatcher">A <see cref="Dispatcher"/> instance that can marshal calls to the required thread or sync context.</param>
		/// <param name="fileProvider">Provides static content to the webview.</param>
		/// <param name="contentRootRelativeToAppRoot">Path to the directory containing application content files.</param>
		/// <param name="hostPageRelativePath">Path to the host page within the <paramref name="fileProvider"/>.</param>
		/// <param name="logger">Logger to send log messages to.</param>
		public AndroidWebKitWebViewManager(AWebView webview, IServiceProvider services, Dispatcher dispatcher, IFileProvider fileProvider, JSComponentConfigurationStore jsComponents, string contentRootRelativeToAppRoot, string hostPageRelativePath, ILogger logger)
			: base(services, dispatcher, AppOriginUri, fileProvider, jsComponents, hostPageRelativePath)
		{
			ArgumentNullException.ThrowIfNull(webview);

#if WEBVIEW2_MAUI
			if (services.GetService<MauiBlazorMarkerService>() is null)
			{
				throw new InvalidOperationException(
					"Unable to find the required services. " +
					$"Please add all the required services by calling '{nameof(IServiceCollection)}.{nameof(BlazorWebViewServiceCollectionExtensions.AddMauiBlazorWebView)}' in the application startup code.");
			}
#endif
			_logger = logger;

			_webview = webview;
			_contentRootRelativeToAppRoot = contentRootRelativeToAppRoot;
		}

		/// <inheritdoc />
		protected override void NavigateCore(Uri absoluteUri)
		{
			_logger.NavigatingToUri(absoluteUri);
			_webview.LoadUrl(absoluteUri.AbsoluteUri);
		}

		/// <inheritdoc />

View on GitHub (pinned to f377ff1c5e)

Solutions

  1. Call builder.UseMauiApp<App>().ConfigureServices(services => services.AddMauiBlazorWebView()) in your MauiProgram.CreateMauiApp method.
  2. If using the fluent API, ensure AddMauiBlazorWebView() is called before the app is built: builder.Services.AddMauiBlazorWebView().
  3. Verify no custom handler registration overrides BlazorWebViewHandler without preserving the service registration step.
  4. Check that you are not passing a manually constructed IServiceProvider to the handler instead of the one from the MAUI host.

Example fix

// before
public static MauiApp CreateMauiApp(MauiAppBuilder builder)
{
    builder.UseMauiApp<App>();
    return builder.Build();
}
// after
public static MauiApp CreateMauiApp(MauiAppBuilder builder)
{
    builder.UseMauiApp<App>()
           .Services.AddMauiBlazorWebView();
    return builder.Build();
}
Defensive patterns

Strategy: validation

Validate before calling

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

Try / catch

try
{
    // code that constructs AndroidWebKitWebViewManager
}
catch (InvalidOperationException ex) when (ex.Message.Contains("AddMauiBlazorWebView"))
{
    // Log the actionable message and ensure AddMauiBlazorWebView is called in MauiProgram
    logger.LogError(ex, "Blazor services not registered. Call AddMauiBlazorWebView in MauiProgram.");
    throw;
}

Prevention

When it happens

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

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

Related errors


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