Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 1, 2025

Add environment variables to IConfiguration by default in WebAssemblyHostBuilder

  • You've read the Contributor Guide and Code of Conduct.
  • You've included unit or integration tests for your change, where applicable.
  • You've included inline docs for your change, where applicable.
  • There's an open issue for the PR that you are making. If you'd like to propose a new feature or change, please open an issue to discuss the change or find an existing issue.

Add environment variables to IConfiguration by default in WebAssemblyHostBuilder.CreateDefault()

Description

Environment variables injected via MonoConfig.environmentVariables are available through Environment.GetEnvironmentVariable() but not in IConfiguration. This blocks Aspire integration since Service Discovery and OpenTelemetry read from IConfiguration.

Changes:

  • Add Microsoft.Extensions.Configuration.EnvironmentVariables package reference
  • Call builder.Configuration.AddEnvironmentVariables() in CreateDefault()
  • Add unit test verifying environment variables appear in configuration

Before (workaround required):

var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.Configuration.AddEnvironmentVariables(); // Manual step needed
var endpoint = builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]; // Now works

After:

var builder = WebAssemblyHostBuilder.CreateDefault(args);
var endpoint = builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]; // Works automatically

Aligns WebAssembly behavior with server-side WebApplication.CreateBuilder().

Fixes ##64576

Original prompt

This section details on the original issue you should resolve

<issue_title>[Blazor] Add environment variables to IConfiguration by default in WebAssemblyHostBuilder</issue_title>
<issue_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:

  1. Service Discovery reads configuration from IConfiguration (e.g., services:weatherapi:https:0)
  2. 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 null

Expected 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

Part of .NET 11.0 Blazor + Aspire integration improvements</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@dotnet-policy-service
Copy link
Contributor

Greetings! You've submitted a PR that modifies code that is shared with https://github.com/dotnet/runtime . Please make sure you synchronize this code with the changes in that repo!

…HostBuilder

Co-authored-by: javiercn <6995051+javiercn@users.noreply.github.com>
Copilot AI changed the title [WIP] Add environment variables to IConfiguration by default Add environment variables to IConfiguration by default in WebAssemblyHostBuilder Dec 1, 2025
Copilot AI requested a review from javiercn December 1, 2025 13:01
Copilot finished work on behalf of javiercn December 1, 2025 13:01
@javiercn javiercn marked this pull request as ready for review December 1, 2025 13:05
@javiercn javiercn requested a review from a team as a code owner December 1, 2025 13:05
Copilot AI review requested due to automatic review settings December 1, 2025 13:05
Copilot finished reviewing on behalf of javiercn December 1, 2025 13:07
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds environment variables to IConfiguration by default in WebAssemblyHostBuilder.CreateDefault(), aligning WebAssembly behavior with server-side ASP.NET Core applications. This enables Aspire integration scenarios where Service Discovery and OpenTelemetry configuration need to read from IConfiguration.

Key changes:

  • Environment variables are now automatically available in IConfiguration via CreateDefault()
  • Eliminates the need for developers to manually call AddEnvironmentVariables()
  • Enables Aspire integration to work out-of-the-box

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
WebAssemblyHostBuilder.cs Adds AddEnvironmentVariables() call in CreateDefault() method
Microsoft.AspNetCore.Components.WebAssembly.csproj Adds package reference to Microsoft.Extensions.Configuration.EnvironmentVariables
WebAssemblyHostBuilderTest.cs Adds test to verify environment variables are accessible through IConfiguration

You can also share your feedback on Copilot code review for a chance to win a $100 gift card. Take the survey.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Blazor] Add environment variables to IConfiguration by default in WebAssemblyHostBuilder

2 participants