Skip to content

Commit 9a1fa23

Browse files
authored
[Blazor] Add environment variables to IConfiguration by default in WebAssemblyHostBuilder (#64578)
1 parent ad9df1a commit 9a1fa23

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

src/Components/WebAssembly/WebAssembly/src/Hosting/WebAssemblyHostBuilder.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ public static WebAssemblyHostBuilder CreateDefault(string[]? args = default)
5252

5353
WebAssemblyCultureProvider.Initialize();
5454

55+
// Add environment variables to configuration by default.
56+
// This aligns WebAssembly behavior with server-side ASP.NET Core applications
57+
// where environment variables are automatically included in IConfiguration.
58+
builder.Configuration.AddEnvironmentVariables();
59+
5560
// Right now we don't have conventions or behaviors that are specific to this method
5661
// however, making this the default for the template allows us to add things like that
5762
// in the future, while giving `new WebAssemblyHostBuilder` as an opt-out of opinionated

src/Components/WebAssembly/WebAssembly/src/Microsoft.AspNetCore.Components.WebAssembly.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
<ItemGroup>
1919
<Reference Include="Microsoft.AspNetCore.Components.Web" />
20+
<Reference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" />
2021
<Reference Include="Microsoft.Extensions.Configuration.Json" />
2122
<Reference Include="Microsoft.Extensions.Configuration.Binder" />
2223
<Reference Include="Microsoft.Extensions.Logging" />

src/Components/WebAssembly/WebAssembly/test/Hosting/WebAssemblyHostBuilderTest.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,4 +310,35 @@ private class CircularServiceB
310310
{
311311
public CircularServiceB(CircularServiceA serviceA) { }
312312
}
313+
314+
[Fact]
315+
public void Configuration_IncludesEnvironmentVariables_WhenAddedExplicitly()
316+
{
317+
// Arrange
318+
var testEnvVarKey = $"TEST_WASM_CONFIG_{Guid.NewGuid():N}";
319+
var testEnvVarValue = "test-value-12345";
320+
321+
try
322+
{
323+
// Set an environment variable before creating the builder
324+
Environment.SetEnvironmentVariable(testEnvVarKey, testEnvVarValue);
325+
326+
var builder = new WebAssemblyHostBuilder(new TestInternalJSImportMethods());
327+
328+
// This mimics what CreateDefault now does
329+
builder.Configuration.AddEnvironmentVariables();
330+
331+
// Act
332+
var host = builder.Build();
333+
334+
// Assert
335+
var configuration = host.Services.GetRequiredService<IConfiguration>();
336+
Assert.Equal(testEnvVarValue, configuration[testEnvVarKey]);
337+
}
338+
finally
339+
{
340+
// Clean up the environment variable
341+
Environment.SetEnvironmentVariable(testEnvVarKey, null);
342+
}
343+
}
313344
}

0 commit comments

Comments
 (0)