diff --git a/src/BlazorWebView/src/Maui/Android/AndroidWebKitWebViewManager.cs b/src/BlazorWebView/src/Maui/Android/AndroidWebKitWebViewManager.cs index d602ded634ec..82921feb3a96 100644 --- a/src/BlazorWebView/src/Maui/Android/AndroidWebKitWebViewManager.cs +++ b/src/BlazorWebView/src/Maui/Android/AndroidWebKitWebViewManager.cs @@ -3,6 +3,7 @@ using System.IO; using Android.Webkit; using Microsoft.AspNetCore.Components.Web; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.FileProviders; using AWebView = Android.Webkit.WebView; using AUri = Android.Net.Uri; @@ -33,6 +34,14 @@ public class AndroidWebKitWebViewManager : WebViewManager public AndroidWebKitWebViewManager(AWebView webview!!, IServiceProvider services, Dispatcher dispatcher, IFileProvider fileProvider, JSComponentConfigurationStore jsComponents, string hostPageRelativePath) : base(services, dispatcher, new Uri(AppOrigin), fileProvider, jsComponents, hostPageRelativePath) { +#if WEBVIEW2_MAUI + if (services.GetService() 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 _webview = webview; } diff --git a/src/BlazorWebView/src/Maui/MauiBlazorMarkerService.cs b/src/BlazorWebView/src/Maui/MauiBlazorMarkerService.cs new file mode 100644 index 000000000000..bcaca9fc3860 --- /dev/null +++ b/src/BlazorWebView/src/Maui/MauiBlazorMarkerService.cs @@ -0,0 +1,9 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +namespace Microsoft.AspNetCore.Components.WebView.Maui +{ + internal class MauiBlazorMarkerService + { + } +} diff --git a/src/BlazorWebView/src/Maui/iOS/IOSWebViewManager.cs b/src/BlazorWebView/src/Maui/iOS/IOSWebViewManager.cs index 66e0cd3590fa..807fa0cb3cd0 100644 --- a/src/BlazorWebView/src/Maui/iOS/IOSWebViewManager.cs +++ b/src/BlazorWebView/src/Maui/iOS/IOSWebViewManager.cs @@ -5,6 +5,7 @@ using System.Text.Encodings.Web; using Foundation; using Microsoft.AspNetCore.Components.Web; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.FileProviders; using UIKit; using WebKit; @@ -33,6 +34,13 @@ public class IOSWebViewManager : WebViewManager public IOSWebViewManager(BlazorWebViewHandler blazorMauiWebViewHandler!!, WKWebView webview!!, IServiceProvider provider, Dispatcher dispatcher, IFileProvider fileProvider, JSComponentConfigurationStore jsComponents, string hostPageRelativePath) : base(provider, dispatcher, new Uri(BlazorWebViewHandler.AppOrigin), fileProvider, jsComponents, hostPageRelativePath) { + if (provider.GetService() 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."); + } + _blazorMauiWebViewHandler = blazorMauiWebViewHandler; _webview = webview; diff --git a/src/BlazorWebView/src/SharedSource/BlazorWebViewServiceCollectionExtensions.cs b/src/BlazorWebView/src/SharedSource/BlazorWebViewServiceCollectionExtensions.cs index 71af7ac47930..353404b1e4fa 100644 --- a/src/BlazorWebView/src/SharedSource/BlazorWebViewServiceCollectionExtensions.cs +++ b/src/BlazorWebView/src/SharedSource/BlazorWebViewServiceCollectionExtensions.cs @@ -37,7 +37,12 @@ public static IServiceCollection AddMauiBlazorWebView(this IServiceCollection se services.AddBlazorWebView(); services.TryAddSingleton(new BlazorWebViewDeveloperTools { Enabled = false }); #if WEBVIEW2_MAUI + services.TryAddSingleton(); services.ConfigureMauiHandlers(static handlers => handlers.AddHandler()); +#elif WEBVIEW2_WINFORMS + services.TryAddSingleton(); +#elif WEBVIEW2_WPF + services.TryAddSingleton(); #endif return services; } diff --git a/src/BlazorWebView/src/SharedSource/WebView2WebViewManager.cs b/src/BlazorWebView/src/SharedSource/WebView2WebViewManager.cs index b2f540502ebc..9130017a8d43 100644 --- a/src/BlazorWebView/src/SharedSource/WebView2WebViewManager.cs +++ b/src/BlazorWebView/src/SharedSource/WebView2WebViewManager.cs @@ -18,19 +18,20 @@ #if WEBVIEW2_WINFORMS using System.Diagnostics; using Microsoft.AspNetCore.Components.WebView.WindowsForms; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Web.WebView2; using Microsoft.Web.WebView2.Core; using WebView2Control = Microsoft.Web.WebView2.WinForms.WebView2; -using Microsoft.Extensions.DependencyInjection; #elif WEBVIEW2_WPF using System.Diagnostics; using Microsoft.AspNetCore.Components.WebView.Wpf; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Web.WebView2; using Microsoft.Web.WebView2.Core; using WebView2Control = Microsoft.Web.WebView2.Wpf.WebView2; -using Microsoft.Extensions.DependencyInjection; #elif WEBVIEW2_MAUI using Microsoft.AspNetCore.Components.WebView.Maui; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Web.WebView2.Core; using WebView2Control = Microsoft.UI.Xaml.Controls.WebView2; using System.Runtime.InteropServices.WindowsRuntime; @@ -85,6 +86,22 @@ public WebView2WebViewManager( : base(services, dispatcher, new Uri(AppOrigin), fileProvider, jsComponents, hostPageRelativePath) { +#if WEBVIEW2_WINFORMS + if (services.GetService() 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() 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 + _webview = webview; _externalNavigationStarting = externalNavigationStarting; _developerTools = services.GetRequiredService(); @@ -119,6 +136,13 @@ BlazorWebViewHandler blazorWebViewHandler ) : base(services, dispatcher, new Uri(AppOrigin), fileProvider, jsComponents, hostPageRelativePath) { + if (services.GetService() 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."); + } + _webview = webview; _blazorWebViewHandler = blazorWebViewHandler; diff --git a/src/BlazorWebView/src/WindowsForms/WindowsFormsBlazorMarkerService.cs b/src/BlazorWebView/src/WindowsForms/WindowsFormsBlazorMarkerService.cs new file mode 100644 index 000000000000..2ed95d01a103 --- /dev/null +++ b/src/BlazorWebView/src/WindowsForms/WindowsFormsBlazorMarkerService.cs @@ -0,0 +1,9 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +namespace Microsoft.AspNetCore.Components.WebView.WindowsForms +{ + internal class WindowsFormsBlazorMarkerService + { + } +} diff --git a/src/BlazorWebView/src/Wpf/WpfBlazorMarkerService.cs b/src/BlazorWebView/src/Wpf/WpfBlazorMarkerService.cs new file mode 100644 index 000000000000..08801b5e1cde --- /dev/null +++ b/src/BlazorWebView/src/Wpf/WpfBlazorMarkerService.cs @@ -0,0 +1,9 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +namespace Microsoft.AspNetCore.Components.WebView.Wpf +{ + internal class WpfBlazorMarkerService + { + } +}