Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ServiceActivator instead of IHttpContextAccessor #801

Merged
merged 1 commit into from
Oct 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions Oqtane.Client/Modules/Controls/Label.razor
Original file line number Diff line number Diff line change
@@ -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))
{
Expand Down Expand Up @@ -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 = @<text>@localizer[$"{ResourceKey}.Text"]</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 = @<text>@localizer[$"{ResourceKey}.Text"]</text>;
HelpText = localizer[$"{ResourceKey}.{nameof(HelpText)}"];
}
}
}
}
Expand Down
1 change: 0 additions & 1 deletion Oqtane.Client/Oqtane.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
6 changes: 4 additions & 2 deletions Oqtane.Client/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions Oqtane.Server/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down