Use ServiceActivator instead of IHttpContextAccessor

This commit is contained in:
hishamco 2020-10-15 06:07:11 +03:00
parent 81475fd835
commit 4a90e6e64f
4 changed files with 15 additions and 9 deletions

View File

@ -1,8 +1,6 @@
@namespace Oqtane.Modules.Controls @namespace Oqtane.Modules.Controls
@inherits ModuleControlBase @inherits ModuleControlBase
@using Microsoft.AspNetCore.Http
@using Microsoft.Extensions.Localization @using Microsoft.Extensions.Localization
@inject IHttpContextAccessor HttpContextAccessor
@if (!string.IsNullOrEmpty(HelpText)) @if (!string.IsNullOrEmpty(HelpText))
{ {
@ -54,7 +52,11 @@ else
var moduleType = Type.GetType(ModuleState.ModuleType); var moduleType = Type.GetType(ModuleState.ModuleType);
var localizerTypeName = $"Microsoft.Extensions.Localization.IStringLocalizer`1[[{moduleType.AssemblyQualifiedName}]], Microsoft.Extensions.Localization.Abstractions"; var localizerTypeName = $"Microsoft.Extensions.Localization.IStringLocalizer`1[[{moduleType.AssemblyQualifiedName}]], Microsoft.Extensions.Localization.Abstractions";
var localizerType = Type.GetType(localizerTypeName); var localizerType = Type.GetType(localizerTypeName);
var localizer = (IStringLocalizer)HttpContextAccessor.HttpContext.RequestServices.GetService(localizerType);
// 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 = @<text>@localizer[$"{ResourceKey}.Text"]</text>; ChildContent = @<text>@localizer[$"{ResourceKey}.Text"]</text>;
HelpText = localizer[$"{ResourceKey}.{nameof(HelpText)}"]; HelpText = localizer[$"{ResourceKey}.{nameof(HelpText)}"];
@ -62,3 +64,4 @@ else
} }
} }
} }
}

View File

@ -32,7 +32,6 @@
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Build" Version="3.2.0" PrivateAssets="all" /> <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Build" Version="3.2.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="3.2.0" PrivateAssets="all" /> <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="3.2.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.AspNetCore.Components.Authorization" Version="3.1.4" /> <PackageReference Include="Microsoft.AspNetCore.Components.Authorization" Version="3.1.4" />
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.2" />
<PackageReference Include="Microsoft.Extensions.Localization" Version="3.1.3" /> <PackageReference Include="Microsoft.Extensions.Localization" Version="3.1.3" />
<PackageReference Include="System.Net.Http.Json" Version="3.2.0" /> <PackageReference Include="System.Net.Http.Json" Version="3.2.0" />
</ItemGroup> </ItemGroup>

View File

@ -26,7 +26,6 @@ namespace Oqtane.Client
builder.Services.AddSingleton(httpClient); builder.Services.AddSingleton(httpClient);
builder.Services.AddOptions(); builder.Services.AddOptions();
builder.Services.AddHttpContextAccessor();
// Register localization services // Register localization services
builder.Services.AddLocalization(options => options.ResourcesPath = "Resources"); builder.Services.AddLocalization(options => options.ResourcesPath = "Resources");
@ -89,8 +88,11 @@ namespace Oqtane.Client
.ToList() .ToList()
.ForEach(x => x.ConfigureServices(builder.Services)); .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) private static async Task LoadClientAssemblies(HttpClient http)

View File

@ -231,6 +231,8 @@ namespace Oqtane
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{ {
ServiceActivator.Configure(app.ApplicationServices);
if (env.IsDevelopment()) if (env.IsDevelopment())
{ {
app.UseDeveloperExceptionPage(); app.UseDeveloperExceptionPage();