diff --git a/src/Components/Shared/src/BrowserNavigationManagerInterop.cs b/src/Components/Shared/src/BrowserNavigationManagerInterop.cs index 2f7d4d5c7efd..a51bd5720ba2 100644 --- a/src/Components/Shared/src/BrowserNavigationManagerInterop.cs +++ b/src/Components/Shared/src/BrowserNavigationManagerInterop.cs @@ -16,8 +16,6 @@ internal static class BrowserNavigationManagerInterop public const string NavigateTo = Prefix + "navigateTo"; - public const string NavigateToWithArgs = Prefix + "navigateToWithArgs"; - public const string Refresh = Prefix + "refresh"; public const string SetHasLocationChangingListeners = Prefix + "setHasLocationChangingListeners"; diff --git a/src/Components/Web.JS/src/Services/NavigationManager.ts b/src/Components/Web.JS/src/Services/NavigationManager.ts index 57267d2040c9..36f3e88e2902 100644 --- a/src/Components/Web.JS/src/Services/NavigationManager.ts +++ b/src/Components/Web.JS/src/Services/NavigationManager.ts @@ -32,7 +32,6 @@ export const internalFunctions = { setHasLocationChangingListeners, endLocationChanging, navigateTo: navigateToFromDotNet, - navigateToWithArgs: navigateToFromDotNetWithArgs, refresh, getBaseURI: (): string => document.baseURI, getLocationHref: (): string => location.href, @@ -116,10 +115,6 @@ function navigateToFromDotNet(uri: string, options: NavigationOptions): void { navigateToCore(uri, options, /* skipLocationChangingCallback */ true); } -function navigateToFromDotNetWithArgs(uri: string, forceLoad: boolean, replaceHistoryEntry: boolean, historyEntryState: string | null): void { - navigateToCore(uri, { forceLoad, replaceHistoryEntry, historyEntryState: historyEntryState ?? undefined }, /* skipLocationChangingCallback */ true); -} - function navigateToCore(uri: string, options: NavigationOptions, skipLocationChangingCallback = false): void { const absoluteUri = toAbsoluteUri(uri); const pageLoadMechanism = currentPageLoadMechanism(); diff --git a/src/Components/WebAssembly/WebAssembly/src/Hosting/WebAssemblyHostBuilder.cs b/src/Components/WebAssembly/WebAssembly/src/Hosting/WebAssemblyHostBuilder.cs index 6110d24d56aa..70082f42ba41 100644 --- a/src/Components/WebAssembly/WebAssembly/src/Hosting/WebAssemblyHostBuilder.cs +++ b/src/Components/WebAssembly/WebAssembly/src/Hosting/WebAssemblyHostBuilder.cs @@ -338,12 +338,12 @@ internal void InitializeDefaultServices() RegisterPersistentComponentStateServiceCollectionExtensions.AddPersistentServiceRegistration(Services, RenderMode.InteractiveWebAssembly); Services.AddLogging(builder => { - builder.AddProvider(new WebAssemblyConsoleLoggerProvider()); + builder.AddProvider(new WebAssemblyConsoleLoggerProvider(DefaultWebAssemblyJSRuntime.Instance)); }); Services.AddSingleton(); RegisterPersistentComponentStateServiceCollectionExtensions.AddPersistentServiceRegistration(Services, RenderMode.InteractiveWebAssembly); Services.AddSupplyValueFromQueryProvider(); - + // Register metrics and tracing when explicitly enabled (opt-in via feature switch) var isTelemetryEnabled = AppContext.TryGetSwitch("System.Diagnostics.Metrics.Meter.IsSupported", out var switchValue) && switchValue == true; if (isTelemetryEnabled) diff --git a/src/Components/WebAssembly/WebAssembly/src/Services/WebAssemblyConsoleLogger.cs b/src/Components/WebAssembly/WebAssembly/src/Services/WebAssemblyConsoleLogger.cs index 186969184e67..c2328cc47dc2 100644 --- a/src/Components/WebAssembly/WebAssembly/src/Services/WebAssemblyConsoleLogger.cs +++ b/src/Components/WebAssembly/WebAssembly/src/Services/WebAssemblyConsoleLogger.cs @@ -5,6 +5,8 @@ using System.Runtime.InteropServices.JavaScript; using System.Text; using Microsoft.Extensions.Logging; +using Microsoft.JSInterop; +using Microsoft.JSInterop.WebAssembly; namespace Microsoft.AspNetCore.Components.WebAssembly.Services; @@ -16,15 +18,17 @@ internal sealed class WebAssemblyConsoleLogger : ILogger, ILogger private static readonly StringBuilder _logBuilder = new StringBuilder(); private readonly string _name; + private readonly WebAssemblyJSRuntime _jsRuntime; - public WebAssemblyConsoleLogger() - : this(string.Empty) + public WebAssemblyConsoleLogger(IJSRuntime jsRuntime) + : this(string.Empty, (WebAssemblyJSRuntime)jsRuntime) // Cast for DI { } - public WebAssemblyConsoleLogger(string name) + public WebAssemblyConsoleLogger(string name, WebAssemblyJSRuntime jsRuntime) { _name = name ?? throw new ArgumentNullException(nameof(name)); + _jsRuntime = jsRuntime ?? throw new ArgumentNullException(nameof(jsRuntime)); } public IDisposable? BeginScope(TState state) where TState : notnull @@ -54,7 +58,7 @@ public void Log(LogLevel logLevel, EventId eventId, TState state, Except } } - private static void WriteMessage(LogLevel logLevel, string logName, int eventId, string message, Exception? exception) + private void WriteMessage(LogLevel logLevel, string logName, int eventId, string message, Exception? exception) { lock (_logBuilder) { @@ -89,7 +93,7 @@ private static void WriteMessage(LogLevel logLevel, string logName, int eventId, break; default: // invalid enum values Debug.Assert(logLevel != LogLevel.None, "This method is never called with LogLevel.None."); - ConsoleLoggerInterop.ConsoleLog(formattedMessage); + _jsRuntime.InvokeVoid("console.log", formattedMessage); break; } } @@ -162,8 +166,6 @@ public void Dispose() { } internal static partial class ConsoleLoggerInterop { - [JSImport("globalThis.console.log")] - public static partial void ConsoleLog(string message); [JSImport("globalThis.console.debug")] public static partial void ConsoleDebug(string message); [JSImport("globalThis.console.info")] diff --git a/src/Components/WebAssembly/WebAssembly/src/Services/WebAssemblyConsoleLoggerProvider.cs b/src/Components/WebAssembly/WebAssembly/src/Services/WebAssemblyConsoleLoggerProvider.cs index 75f541bca610..6d4c62213ab0 100644 --- a/src/Components/WebAssembly/WebAssembly/src/Services/WebAssemblyConsoleLoggerProvider.cs +++ b/src/Components/WebAssembly/WebAssembly/src/Services/WebAssemblyConsoleLoggerProvider.cs @@ -3,6 +3,7 @@ using System.Collections.Concurrent; using Microsoft.Extensions.Logging; +using Microsoft.JSInterop.WebAssembly; namespace Microsoft.AspNetCore.Components.WebAssembly.Services; @@ -11,12 +12,22 @@ namespace Microsoft.AspNetCore.Components.WebAssembly.Services; /// internal sealed class WebAssemblyConsoleLoggerProvider : ILoggerProvider { - private readonly ConcurrentDictionary> _loggers = new(); + private readonly ConcurrentDictionary> _loggers; + private readonly WebAssemblyJSRuntime _jsRuntime; + + /// + /// Creates an instance of . + /// + public WebAssemblyConsoleLoggerProvider(WebAssemblyJSRuntime jsRuntime) + { + _loggers = new ConcurrentDictionary>(); + _jsRuntime = jsRuntime; + } /// public ILogger CreateLogger(string name) { - return _loggers.GetOrAdd(name, static loggerName => new WebAssemblyConsoleLogger(loggerName)); + return _loggers.GetOrAdd(name, loggerName => new WebAssemblyConsoleLogger(name, _jsRuntime)); } /// diff --git a/src/Components/WebAssembly/WebAssembly/src/Services/WebAssemblyNavigationManager.cs b/src/Components/WebAssembly/WebAssembly/src/Services/WebAssemblyNavigationManager.cs index 461a0604c2e3..401938dbe076 100644 --- a/src/Components/WebAssembly/WebAssembly/src/Services/WebAssemblyNavigationManager.cs +++ b/src/Components/WebAssembly/WebAssembly/src/Services/WebAssemblyNavigationManager.cs @@ -1,10 +1,11 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using System.Runtime.InteropServices.JavaScript; +using System.Diagnostics.CodeAnalysis; using Microsoft.AspNetCore.Components.Routing; -using Microsoft.AspNetCore.Components.Web; using Microsoft.Extensions.Logging; +using Microsoft.JSInterop; +using Interop = Microsoft.AspNetCore.Components.Web.BrowserNavigationManagerInterop; namespace Microsoft.AspNetCore.Components.WebAssembly.Services; @@ -48,6 +49,7 @@ public async ValueTask HandleLocationChangingAsync(string uri, string? sta } /// + [DynamicDependency(DynamicallyAccessedMemberTypes.PublicProperties, typeof(NavigationOptions))] protected override void NavigateToCore(string uri, NavigationOptions options) { ArgumentNullException.ThrowIfNull(uri); @@ -66,7 +68,7 @@ async Task PerformNavigationAsync() return; } - NavigationManagerInterop.NavigateTo(uri, options.ForceLoad, options.ReplaceHistoryEntry, options.HistoryEntryState); + DefaultWebAssemblyJSRuntime.Instance.InvokeVoid(Interop.NavigateTo, uri, options); } catch (Exception ex) { @@ -80,7 +82,7 @@ async Task PerformNavigationAsync() /// public override void Refresh(bool forceReload = false) { - NavigationManagerInterop.Refresh(forceReload); + DefaultWebAssemblyJSRuntime.Instance.InvokeVoid(Interop.Refresh, forceReload); } protected override void HandleLocationChangingHandlerException(Exception ex, LocationChangingContext context) @@ -100,12 +102,3 @@ private static partial class Log public static partial void NavigationFailed(ILogger logger, string uri, Exception exception); } } - -internal static partial class NavigationManagerInterop -{ - [JSImport(BrowserNavigationManagerInterop.NavigateToWithArgs, "blazor-internal")] - public static partial void NavigateTo(string uri, bool forceLoad, bool replaceHistoryEntry, string? historyEntryState); - - [JSImport(BrowserNavigationManagerInterop.Refresh, "blazor-internal")] - public static partial void Refresh(bool forceReload); -} diff --git a/src/Components/test/testassets/BasicTestApp/PrependMessageLoggerProvider.cs b/src/Components/test/testassets/BasicTestApp/PrependMessageLoggerProvider.cs index 5acdae8626f6..e2353a827484 100644 --- a/src/Components/test/testassets/BasicTestApp/PrependMessageLoggerProvider.cs +++ b/src/Components/test/testassets/BasicTestApp/PrependMessageLoggerProvider.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using Microsoft.AspNetCore.Components.WebAssembly.Services; +using Microsoft.JSInterop; namespace BasicTestApp; @@ -13,10 +14,10 @@ internal class PrependMessageLoggerProvider : ILoggerProvider readonly ILogger _defaultLogger; private bool _disposed = false; - public PrependMessageLoggerProvider(string message) + public PrependMessageLoggerProvider(string message, IJSRuntime runtime) { _message = message; - _defaultLogger = new WebAssemblyConsoleLogger(); + _defaultLogger = new WebAssemblyConsoleLogger(runtime); } public ILogger CreateLogger(string categoryName) diff --git a/src/Components/test/testassets/BasicTestApp/Program.cs b/src/Components/test/testassets/BasicTestApp/Program.cs index ed788f5e62fe..0332b3915c88 100644 --- a/src/Components/test/testassets/BasicTestApp/Program.cs +++ b/src/Components/test/testassets/BasicTestApp/Program.cs @@ -50,8 +50,8 @@ public static async Task Main(string[] args) builder.Logging.AddConfiguration(builder.Configuration.GetSection("Logging")); - builder.Logging.Services.AddSingleton(_ => - new PrependMessageLoggerProvider(builder.Configuration["Logging:PrependMessage:Message"])); + builder.Logging.Services.AddSingleton(s => + new PrependMessageLoggerProvider(builder.Configuration["Logging:PrependMessage:Message"], s.GetService())); var host = builder.Build(); ConfigureCulture(host);