Blazor WebAssembly 3.2.0预览版3-在Program.cs中使用appsettings。{environment} .json

时间:2020-04-02 10:39:48

标签: blazor blazor-client-side

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

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

我可以在var host = builder.Build();之后获得配置(Microsoft Docs提供的 info,请参见下面的示例代码,以及上文link此处),但是在此之前调用了gRPC服务。

More info关于appsetting。{environment} .json在Blazor WebAssembly 3.2.0 Preview 3中

我的问题:是否可以继续使用#if DEBUG等。
我希望在代码中的任何地方都可以使用appsettings。

我的Program.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();

2 个答案:

答案 0 :(得分:1)

我也在GitHub dotnet / aspnetcore上发布了这个问题,詹姆斯·牛顿·金提出了答案,请参阅: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;
});

答案 1 :(得分:1)

基于Jaap's的回答,我必须对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;
        });