Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ public static WebAssemblyHostBuilder CreateDefault(string[]? args = default)

WebAssemblyCultureProvider.Initialize();

// Add environment variables to configuration by default.
// This aligns WebAssembly behavior with server-side ASP.NET Core applications
// where environment variables are automatically included in IConfiguration.
builder.Configuration.AddEnvironmentVariables();

// Right now we don't have conventions or behaviors that are specific to this method
// however, making this the default for the template allows us to add things like that
// in the future, while giving `new WebAssemblyHostBuilder` as an opt-out of opinionated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

<ItemGroup>
<Reference Include="Microsoft.AspNetCore.Components.Web" />
<Reference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" />
<Reference Include="Microsoft.Extensions.Configuration.Json" />
<Reference Include="Microsoft.Extensions.Configuration.Binder" />
<Reference Include="Microsoft.Extensions.Logging" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,4 +310,35 @@ private class CircularServiceB
{
public CircularServiceB(CircularServiceA serviceA) { }
}

[Fact]
public void Configuration_IncludesEnvironmentVariables_WhenAddedExplicitly()
{
// Arrange
var testEnvVarKey = $"TEST_WASM_CONFIG_{Guid.NewGuid():N}";
var testEnvVarValue = "test-value-12345";

try
{
// Set an environment variable before creating the builder
Environment.SetEnvironmentVariable(testEnvVarKey, testEnvVarValue);

var builder = new WebAssemblyHostBuilder(new TestInternalJSImportMethods());

// This mimics what CreateDefault now does
builder.Configuration.AddEnvironmentVariables();

// Act
var host = builder.Build();

// Assert
var configuration = host.Services.GetRequiredService<IConfiguration>();
Assert.Equal(testEnvVarValue, configuration[testEnvVarKey]);
}
finally
{
// Clean up the environment variable
Environment.SetEnvironmentVariable(testEnvVarKey, null);
}
}
}
Loading