Use ServiceActivator instead of IHttpContextAccessor
This commit is contained in:
parent
81475fd835
commit
4a90e6e64f
|
@ -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,7 +52,11 @@ 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);
|
||||
|
||||
// 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>;
|
||||
HelpText = localizer[$"{ResourceKey}.{nameof(HelpText)}"];
|
||||
|
@ -62,3 +64,4 @@ else
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -32,7 +32,6 @@
|
|||
<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.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="System.Net.Http.Json" Version="3.2.0" />
|
||||
</ItemGroup>
|
||||
|
|
|
@ -26,7 +26,6 @@ namespace Oqtane.Client
|
|||
|
||||
builder.Services.AddSingleton(httpClient);
|
||||
builder.Services.AddOptions();
|
||||
builder.Services.AddHttpContextAccessor();
|
||||
|
||||
// Register localization services
|
||||
builder.Services.AddLocalization(options => options.ResourcesPath = "Resources");
|
||||
|
@ -89,8 +88,11 @@ namespace Oqtane.Client
|
|||
.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)
|
||||
|
|
|
@ -231,6 +231,8 @@ namespace Oqtane
|
|||
// 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();
|
||||
|
|
Loading…
Reference in New Issue
Block a user