-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Description
Summary
Environment variables injected into WebAssembly via MonoConfig.environmentVariables should be automatically available in IConfiguration without requiring an explicit call to AddEnvironmentVariables().
Background
In Blazor WebAssembly, environment variables can be injected via JavaScript initializers into MonoConfig.environmentVariables. These variables are then available via Environment.GetEnvironmentVariable(), but they are NOT automatically included in IConfiguration.
This is problematic for Aspire integration because:
- Service Discovery reads configuration from
IConfiguration(e.g.,services:weatherapi:https:0) - OpenTelemetry configuration comes from
IConfiguration(e.g.,OTEL_EXPORTER_OTLP_ENDPOINT)
Currently, developers must explicitly add:
var builder = WebAssemblyHostBuilder.CreateDefault(args);
// Required workaround - environment variables are in Environment.GetEnvironmentVariable()
// but NOT in IConfiguration by default
builder.Configuration.AddEnvironmentVariables();Current Behavior
// In WebAssembly after JS initializer injects env vars into MonoConfig.environmentVariables:
// This WORKS:
var endpoint = Environment.GetEnvironmentVariable("OTEL_EXPORTER_OTLP_ENDPOINT"); // ✅ Returns value
// This DOES NOT WORK without explicit AddEnvironmentVariables():
var endpoint = builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]; // ❌ Returns nullExpected Behavior
Environment variables should be available in IConfiguration by default, matching the behavior of server-side ASP.NET Core applications where WebApplication.CreateBuilder() includes environment variables automatically.
Proposed Solution
In WebAssemblyHostBuilder.CreateDefault(), add EnvironmentVariablesConfigurationProvider to the default configuration sources:
public static WebAssemblyHostBuilder CreateDefault(string[]? args = null)
{
var builder = new WebAssemblyHostBuilder(...);
// Add environment variables to configuration by default
builder.Configuration.AddEnvironmentVariables();
// ... rest of initialization
return builder;
}Impact
This change would:
- Align WebAssembly behavior with server-side ASP.NET Core
- Enable Service Discovery to work out-of-the-box with Aspire
- Enable OpenTelemetry configuration to work without additional setup
- Reduce boilerplate for Aspire integration
Workaround
Until this is addressed, add the following to your Blazor WASM Program.cs:
builder.Configuration.AddEnvironmentVariables();Note: This requires adding a package reference to Microsoft.Extensions.Configuration.EnvironmentVariables.
Related Issues
- [Blazor] Provide the ability to flow configuration from the host to the browser #30116 - Server-side configuration endpoint for Blazor Web
- Support hosted services in WebAssemblyHost #63637 - IHostedService support in WebAssembly
- [Blazor] Gateway package for standalone Blazor WebAssembly applications #64573 - Gateway package for standalone Blazor WebAssembly
- [Blazor] WebAssembly service defaults template for Aspire integration #64574 - WebAssembly service defaults template