title: Blazor WebAssembly 3.2.0 Preview 3 - Using appsettings.{environment}.json in Program.cs
body: <p>I'm using Blazor WebAssembly 3.2.0 Preview 3 / Static / Client Side</p>
  
  <ul>
  <li>Blazor .gRPC <code>Program.cs</code> Code example: <a href="https://github.com/grpc/grpc-dotnet/tree/master/examples/Blazor" rel="nofollow noreferrer">https://github.com/grpc/grpc-dotnet/tree/master/examples/Blazor</a></li>
  <li>Docs Blazor Wasm appsettings page: <a href="https://docs.microsoft.com/en-us/aspnet/core/blazor/dependency-injection?view=aspnetcore-3.1#blazor-webassembly" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/aspnet/core/blazor/dependency-injection?view=aspnetcore-3.1#blazor-webassembly</a></li>
  </ul>
  
  <p>I'm now using <code>#if DEBUG #else #endif</code> for the string variable "<code>backendUrl</code>". I'd like to load that setting from appsettings.{environment}.json. </p>
  
  <p>I can get the config after <code>var host = builder.Build();</code> (<em>info from Microsoft Docs, see sample code below, and <a href="https://docs.microsoft.com/en-us/aspnet/core/blazor/dependency-injection?view=aspnetcore-3.1#blazor-webassembly" rel="nofollow noreferrer">link</a> here above</em>) but the gRPC service is called before that. </p>
  
  <p><a href="https://devblogs.microsoft.com/aspnet/blazor-webassembly-3-2-0-preview-3-release-now-available/" rel="nofollow noreferrer">More info</a> about appsetting.{environment}.json in Blazor WebAssembly 3.2.0 Preview 3</p>
  
  <p>My question: is it possible or should I keep using <code>#if DEBUG</code> etc. <br />(<em>I'd like to use appsettings whenever it is possible anywhere in my code.</em>)</p>
  
  <p><strong>Part of my Program.cs</strong></p>
  
  <pre><code>    string backendUrl = string.Empty;
  #if DEBUG
      backendUrl = "https://localhost:5001"; // Local debug URL
  #else
      backendUrl = "https://&lt;example&gt;.com:5001"; // Production URL
  #endif
      builder.Services.AddSingleton(services =&gt;
      {
          // 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();
  </code></pre>