I'm using Blazor WebAssembly 3.2.0 Preview 3 / Static / Client Side
- Blazor .gRPC
Program.cs
Code example: https://github.com/grpc/grpc-dotnet/tree/master/examples/Blazor - Docs Blazor Wasm appsettings page: https://docs.microsoft.com/en-us/aspnet/core/blazor/dependency-injection?view=aspnetcore-3.1#blazor-webassembly
I'm now using #if DEBUG #else #endif
for the string variable "backendUrl
". I'd like to load that setting from appsettings.{environment}.json.
I can get the config after var host = builder.Build();
(info from Microsoft Docs, see sample code below, and link here above) but the gRPC service is called before that.
More info about appsetting.{environment}.json in Blazor WebAssembly 3.2.0 Preview 3
My question: is it possible or should I keep using #if DEBUG
etc.
(I'd like to use appsettings whenever it is possible anywhere in my code.)
Part of my 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();