File tree Expand file tree Collapse file tree 3 files changed +37
-0
lines changed
src/Components/WebAssembly/WebAssembly Expand file tree Collapse file tree 3 files changed +37
-0
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 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" />
Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments