Blazor WebAssembly 3.2.0预览3-使用应用程序设置.Program.cs中的{environmen



我正在使用Blazor WebAssembly 3.2.0预览3/静态/客户端

  • Blazor.gRPCProgram.cs代码示例:https://github.com/grpc/grpc-dotnet/tree/master/examples/Blazor
  • Docs Blazor Wasm应用程序设置页面:https://learn.microsoft.com/en-us/aspnet/core/blazor/dependency-injection?view=aspnetcore-3.1#blazor webassembly

我现在使用#if DEBUG #else #endif作为字符串变量"backendUrl"。我想从appsettings加载该设置。{environment}.json.

我可以在var host = builder.Build();之后获得配置(来自Microsoft Docs的信息,请参阅下面的示例代码,以及上面的链接(,但在此之前调用了gRPC服务。

有关应用程序设置的详细信息。{environment}.json在Blazor WebAssembly 3.2.0预览3

我的问题是:有可能还是应该继续使用#if DEBUG等。
(我想在代码中的任何地方尽可能使用appsettings。(

我的程序的一部分。cs

string backendUrl = string.Empty;
#if DEBUG
backendUrl = "https://localhost:5001"; // Local debug URL
#else
backendUrl = "https://<example>.com:5001"; // Production URL
#endif
builder.Services.AddSingleton(services =>
{
// Create a gRPC-Web channel pointing to the backend server.
// GrpcWebText is used because server streaming requires it. If server streaming is not used in your app
// then GrpcWeb is recommended because it produces smaller messages.
var httpClient = new HttpClient(new GrpcWebHandler(GrpcWebMode.GrpcWebText, new HttpClientHandler()));
var channel = GrpcChannel.ForAddress(backendUrl, new GrpcChannelOptions { HttpClient = httpClient });
return channel;
});
// load settings from appsettings.{environment}.json
// see: https://learn.microsoft.com/en-us/aspnet/core/blazor/dependency-injection?view=aspnetcore-3.1#add-services-to-an-app
var host = builder.Build();
var backendDomain = host.Configuration["Settings:BackEndDomain"];
Console.WriteLine($"Backend Domain: {backendDomain}");
await host.RunAsync();
// original
// await builder.Build().RunAsync();

我也在GitHub dotnet/aspnetcore上发布了这个问题,James Newton King给出了答案,请参阅:https://github.com/dotnet/aspnetcore/issues/20442#issuecomment-608064432

JamesNK:

您应该能够在AddSingleton中获得IConfiguration。例如

builder.Services.AddSingleton(services =>
{
var configuration = services.GetRequiredService<IConfiguration>();
var backendUrl = configuration["BackendUrl"];
// Create a gRPC-Web channel pointing to the backend server.
// GrpcWebText is used because server streaming requires it. If server streaming is not used in your app
// then GrpcWeb is recommended because it produces smaller messages.
var httpClient = new HttpClient(new GrpcWebHandler(GrpcWebMode.GrpcWebText, new HttpClientHandler()));
var channel = GrpcChannel.ForAddress(backendUrl, new GrpcChannelOptions { HttpClient = httpClient });
return channel;
});

根据Jaap的回答,我必须为Serilog BrowserHttp Sink端点执行以下操作

services.AddSingleton(provider =>
{
var config = provider.GetService<IConfiguration>();
_appConfiguration = config.GetSection("App").Get<AppConfiguration>();
var levelSwitch = new LoggingLevelSwitch();
Log.Logger = new LoggerConfiguration()
.MinimumLevel.ControlledBy(levelSwitch)
.Enrich.WithProperty("InstanceId", Guid.NewGuid().ToString("n"))
.WriteTo.BrowserHttp(_appConfiguration.ApiBaseUrl, controlLevelSwitch: levelSwitch)
.WriteTo.BrowserConsole()
.CreateLogger();
Log.Information("Hello, browser!");
return _appConfiguration;
});

最新更新