99using Microsoft . AspNetCore . StaticFiles ;
1010using Microsoft . Extensions . DependencyInjection ;
1111using Microsoft . Extensions . FileProviders ;
12- using Microsoft . Extensions . Hosting ;
1312using Microsoft . Extensions . Primitives ;
1413using Microsoft . Net . Http . Headers ;
1514
@@ -20,6 +19,12 @@ namespace Microsoft.AspNetCore.Builder;
2019/// </summary>
2120public static class ComponentsWebAssemblyApplicationBuilderExtensions
2221{
22+ private static readonly string ? s_dotnetModifiableAssemblies = GetNonEmptyEnvironmentVariableValue ( "DOTNET_MODIFIABLE_ASSEMBLIES" ) ;
23+ private static readonly string ? s_aspnetcoreBrowserTools = GetNonEmptyEnvironmentVariableValue ( "__ASPNETCORE_BROWSER_TOOLS" ) ;
24+
25+ private static string ? GetNonEmptyEnvironmentVariableValue ( string name )
26+ => Environment . GetEnvironmentVariable ( name ) is { Length : > 0 } value ? value : null ;
27+
2328 /// <summary>
2429 /// Configures the application to serve Blazor WebAssembly framework files from the path <paramref name="pathPrefix"/>. This path must correspond to a referenced Blazor WebAssembly application project.
2530 /// </summary>
@@ -42,23 +47,19 @@ public static IApplicationBuilder UseBlazorFrameworkFiles(this IApplicationBuild
4247 {
4348 context . Response . Headers . Append ( "Blazor-Environment" , webHostEnvironment . EnvironmentName ) ;
4449
45- if ( webHostEnvironment . IsDevelopment ( ) )
50+ // DOTNET_MODIFIABLE_ASSEMBLIES is used by the runtime to initialize hot-reload specific environment variables and is configured
51+ // by the launching process (dotnet-watch / Visual Studio).
52+ // Always add the header if the environment variable is set, regardless of the kind of environment.
53+ if ( s_dotnetModifiableAssemblies != null )
54+ {
55+ context . Response . Headers . Append ( "DOTNET-MODIFIABLE-ASSEMBLIES" , s_dotnetModifiableAssemblies ) ;
56+ }
57+
58+ // See https://github.com/dotnet/aspnetcore/issues/37357#issuecomment-941237000
59+ // Translate the _ASPNETCORE_BROWSER_TOOLS environment configured by the browser tools agent in to a HTTP response header.
60+ if ( s_aspnetcoreBrowserTools != null )
4661 {
47- // DOTNET_MODIFIABLE_ASSEMBLIES is used by the runtime to initialize hot-reload specific environment variables and is configured
48- // by the launching process (dotnet-watch / Visual Studio).
49- // In Development, we'll transmit the environment variable to WebAssembly as a HTTP header. The bootstrapping code will read the header
50- // and configure it as env variable for the wasm app.
51- if ( Environment . GetEnvironmentVariable ( "DOTNET_MODIFIABLE_ASSEMBLIES" ) is string dotnetModifiableAssemblies )
52- {
53- context . Response . Headers . Append ( "DOTNET-MODIFIABLE-ASSEMBLIES" , dotnetModifiableAssemblies ) ;
54- }
55-
56- // See https://github.com/dotnet/aspnetcore/issues/37357#issuecomment-941237000
57- // Translate the _ASPNETCORE_BROWSER_TOOLS environment configured by the browser tools agent in to a HTTP response header.
58- if ( Environment . GetEnvironmentVariable ( "__ASPNETCORE_BROWSER_TOOLS" ) is string blazorWasmHotReload )
59- {
60- context . Response . Headers . Append ( "ASPNETCORE-BROWSER-TOOLS" , blazorWasmHotReload ) ;
61- }
62+ context . Response . Headers . Append ( "ASPNETCORE-BROWSER-TOOLS" , s_aspnetcoreBrowserTools ) ;
6263 }
6364
6465 await next ( context ) ;
0 commit comments