Blazor WebAssembly 3.2.0预览3-在Program.cs中使用appsettings.{environment}.json
我正在使用Blazor WebAssembly 3.2.0预览版3/静态/客户端Blazor WebAssembly 3.2.0预览3-在Program.cs中使用appsettings.{environment}.json,blazor,blazor-client-side,Blazor,Blazor Client Side,我正在使用Blazor WebAssembly 3.2.0预览版3/静态/客户端 Blazor.gRPCProgram.cs代码示例: Docs Blazor Wasm应用程序设置页面: 我现在对字符串变量“backendUrl”使用#if DEBUG#else#endif”。我想从appsettings.{environment}.json加载该设置 我可以在var host=builder.Build()之后获得配置(来自Microsoft文档的信息,请参见下面和上面的示例代码),但在此
- Blazor.gRPC
代码示例:Program.cs
- Docs Blazor Wasm应用程序设置页面:
backendUrl
”使用#if DEBUG#else#endif
”。我想从appsettings.{environment}.json加载该设置
我可以在var host=builder.Build()之后获得配置代码>(来自Microsoft文档的信息,请参见下面和上面的示例代码),但在此之前会调用gRPC服务
关于Blazor WebAssembly 3.2.0预览版3中的appsetting.{environment}.json
我的问题:是否可能,或者我是否应该继续使用#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://docs.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();
string backendUrl=string.Empty;
#如果调试
后端URL=”https://localhost:5001"; // 本地调试URL
#否则
后端URL=”https://.com:5001"; // 生产URL
#恩迪夫
builder.Services.AddSingleton(服务=>
{
//创建指向后端服务器的gRPC Web频道。
//使用GrpcWebText是因为服务器流需要它。如果应用程序中未使用服务器流
//然后建议使用GrpcWeb,因为它会生成较小的消息。
var httpClient=新的httpClient(新的GrpcWebHandler(GrpcWebMode.GrpcWebText,新的HttpClientHandler());
var channel=GrpcChannel.ForAddress(后端URL,新的GrpcChannelOptions{HttpClient=HttpClient});
返回通道;
});
//从appsettings.{environment}.json加载设置
//见:https://docs.microsoft.com/en-us/aspnet/core/blazor/dependency-injection?view=aspnetcore-3.1#向应用程序添加服务
var host=builder.Build();
var backendDomain=host.Configuration[“Settings:backendDomain”];
WriteLine($“后端域:{backendDomain}”);
等待host.RunAsync();
//原创的
//等待builder.Build().RunAsync();
我还在GitHub dotnet/aspnetcore上发布了这个问题,James Newton King给出了答案,请参见:
JamesNK:
您应该能够在AddSingleton中获得IConfiguration。e、 g
builder.Services.AddSingleton(服务=>
{
var configuration=services.GetRequiredService();
var backendUrl=configuration[“backendUrl”];
//创建指向后端服务器的gRPC Web频道。
//使用GrpcWebText是因为服务器流需要它。如果应用程序中未使用服务器流
//然后建议使用GrpcWeb,因为它会生成较小的消息。
var httpClient=新的httpClient(新的GrpcWebHandler(GrpcWebMode.GrpcWebText,新的HttpClientHandler());
var channel=GrpcChannel.ForAddress(后端URL,新的GrpcChannelOptions{HttpClient=HttpClient});
返回通道;
});
我还在GitHub dotnet/aspnetcore上发布了这个问题,James Newton King给出了答案,请参见:
JamesNK:
您应该能够在AddSingleton中获得IConfiguration。e、 g
builder.Services.AddSingleton(服务=>
{
var configuration=services.GetRequiredService();
var backendUrl=configuration[“backendUrl”];
//创建指向后端服务器的gRPC Web频道。
//使用GrpcWebText是因为服务器流需要它。如果应用程序中未使用服务器流
//然后建议使用GrpcWeb,因为它会生成较小的消息。
var httpClient=新的httpClient(新的GrpcWebHandler(GrpcWebMode.GrpcWebText,新的HttpClientHandler());
var channel=GrpcChannel.ForAddress(后端URL,新的GrpcChannelOptions{HttpClient=HttpClient});
返回通道;
});
根据答案,我必须为Serilog BrowserHttp接收器端点执行以下操作
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;
});
services.AddSingleton(提供者=>
{
var config=provider.GetService();
_appConfiguration=config.GetSection(“App”).Get();
var levelSwitch=新的日志levelSwitch();
Logger.Logger=新的LoggerConfiguration()
.最低液位由(液位开关)控制
.Enrich.WithProperty(“InstanceId”,Guid.NewGuid().ToString(“n”))
.WriteTo.BrowserHttp(_appConfiguration.ApiBaseUrl,controlLevelSwitch:levelSwitch)
.WriteTo.BrowserConsole()
.CreateLogger();
Log.Information(“你好,浏览器!”);
返回appConfiguration;
});
根据答案,我必须为Serilog BrowserHttp接收器端点执行以下操作
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;
});
services.AddSingleton(提供者=>
{
var config=provider.GetService();
_appConfiguration=config.GetSection(“App”).Get();
var levelSwitch=新的日志levelSwitch();
Logger.Logger=新的LoggerConfiguration()
.最低液位由(液位开关)控制
.Enrich.WithProperty(“InstanceId”,Guid.NewGuid().ToString(“n”))
.WriteTo.BrowserHttp(_appConfiguration.ApiBaseUrl,controlLevelSwitch:levelSwitch)
.WriteTo.BrowserConsole()
.CreateLogger();
Log.Information(“你好,浏览器!”);
返回appConfiguration;
});