From 4a90e6e64fc705de3566651ac2d3771bb269fcb6 Mon Sep 17 00:00:00 2001 From: hishamco Date: Thu, 15 Oct 2020 06:07:11 +0300 Subject: [PATCH] Use ServiceActivator instead of IHttpContextAccessor --- Oqtane.Client/Modules/Controls/Label.razor | 15 +++++++++------ Oqtane.Client/Oqtane.Client.csproj | 1 - Oqtane.Client/Program.cs | 6 ++++-- Oqtane.Server/Startup.cs | 2 ++ 4 files changed, 15 insertions(+), 9 deletions(-) diff --git a/Oqtane.Client/Modules/Controls/Label.razor b/Oqtane.Client/Modules/Controls/Label.razor index edccb9ded..0d32a14bb 100644 --- a/Oqtane.Client/Modules/Controls/Label.razor +++ b/Oqtane.Client/Modules/Controls/Label.razor @@ -1,8 +1,6 @@ @namespace Oqtane.Modules.Controls @inherits ModuleControlBase -@using Microsoft.AspNetCore.Http @using Microsoft.Extensions.Localization -@inject IHttpContextAccessor HttpContextAccessor @if (!string.IsNullOrEmpty(HelpText)) { @@ -54,10 +52,15 @@ else var moduleType = Type.GetType(ModuleState.ModuleType); var localizerTypeName = $"Microsoft.Extensions.Localization.IStringLocalizer`1[[{moduleType.AssemblyQualifiedName}]], Microsoft.Extensions.Localization.Abstractions"; var localizerType = Type.GetType(localizerTypeName); - var localizer = (IStringLocalizer)HttpContextAccessor.HttpContext.RequestServices.GetService(localizerType); - - ChildContent = @@localizer[$"{ResourceKey}.Text"]; - HelpText = localizer[$"{ResourceKey}.{nameof(HelpText)}"]; + + // HACK: Use ServiceActivator instead of injecting IHttpContextAccessor, because HttpContext throws NRE in WebAssembly runtime + using (var scope = ServiceActivator.GetScope()) + { + var localizer = (IStringLocalizer)scope.ServiceProvider.GetService(localizerType); + + ChildContent = @@localizer[$"{ResourceKey}.Text"]; + HelpText = localizer[$"{ResourceKey}.{nameof(HelpText)}"]; + } } } } diff --git a/Oqtane.Client/Oqtane.Client.csproj b/Oqtane.Client/Oqtane.Client.csproj index eaff964ab..515bf8f55 100644 --- a/Oqtane.Client/Oqtane.Client.csproj +++ b/Oqtane.Client/Oqtane.Client.csproj @@ -32,7 +32,6 @@ - diff --git a/Oqtane.Client/Program.cs b/Oqtane.Client/Program.cs index 7d3ea0a77..d442141d4 100644 --- a/Oqtane.Client/Program.cs +++ b/Oqtane.Client/Program.cs @@ -26,7 +26,6 @@ public static async Task Main(string[] args) builder.Services.AddSingleton(httpClient); builder.Services.AddOptions(); - builder.Services.AddHttpContextAccessor(); // Register localization services builder.Services.AddLocalization(options => options.ResourcesPath = "Resources"); @@ -89,8 +88,11 @@ public static async Task Main(string[] args) .ToList() .ForEach(x => x.ConfigureServices(builder.Services)); } + var host = builder.Build(); - await builder.Build().RunAsync(); + ServiceActivator.Configure(host.Services); + + await host.RunAsync(); } private static async Task LoadClientAssemblies(HttpClient http) diff --git a/Oqtane.Server/Startup.cs b/Oqtane.Server/Startup.cs index f5c5e07b5..2cff1aa09 100644 --- a/Oqtane.Server/Startup.cs +++ b/Oqtane.Server/Startup.cs @@ -231,6 +231,8 @@ public void ConfigureServices(IServiceCollection services) // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { + ServiceActivator.Configure(app.ApplicationServices); + if (env.IsDevelopment()) { app.UseDeveloperExceptionPage();