Skip to content

Commit 0a84b5e

Browse files
committed
[ANCM] Add switch to prefer env over web.config (#19746)
1 parent e81033e commit 0a84b5e

File tree

4 files changed

+42
-1
lines changed

4 files changed

+42
-1
lines changed

src/Servers/IIS/AspNetCoreModuleV2/RequestHandlerLib/environmentvariablehash.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#define ASPNETCORE_IIS_AUTH_BASIC L"basic;"
1414
#define ASPNETCORE_IIS_AUTH_ANONYMOUS L"anonymous;"
1515
#define ASPNETCORE_IIS_AUTH_NONE L"none"
16+
#define ANCM_PREFER_ENVIRONMENT_VARIABLES_ENV_STR L"ANCM_PREFER_ENVIRONMENT_VARIABLES"
1617

1718
//
1819
// The key used for hash-table lookups, consists of the port on which the http process is created.

src/Servers/IIS/AspNetCoreModuleV2/RequestHandlerLib/environmentvariablehelpers.h

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,27 @@ class ENVIRONMENT_VAR_HELPERS
7878
environmentVariables.insert_or_assign(HOSTING_STARTUP_ASSEMBLIES_ENV_STR, hostingStartupValues);
7979
}
8080

81+
auto preferEnvironmentVariablesSetting = Environment::GetEnvironmentVariableValue(ANCM_PREFER_ENVIRONMENT_VARIABLES_ENV_STR).value_or(L"false");
82+
auto preferEnvironmentVariables = equals_ignore_case(L"1", preferEnvironmentVariablesSetting) || equals_ignore_case(L"true", preferEnvironmentVariablesSetting);
83+
8184
for (auto& environmentVariable : environmentVariables)
8285
{
83-
environmentVariable.second = Environment::ExpandEnvironmentVariables(environmentVariable.second);
86+
if (preferEnvironmentVariables)
87+
{
88+
auto env = Environment::GetEnvironmentVariableValue(environmentVariable.first);
89+
if (env.has_value())
90+
{
91+
environmentVariable.second = env.value();
92+
}
93+
else
94+
{
95+
environmentVariable.second = Environment::ExpandEnvironmentVariables(environmentVariable.second);
96+
}
97+
}
98+
else
99+
{
100+
environmentVariable.second = Environment::ExpandEnvironmentVariables(environmentVariable.second);
101+
}
84102
}
85103

86104
return environmentVariables;

src/Servers/IIS/IIS/test/Common.FunctionalTests/Inprocess/EnvironmentVariableTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,5 +126,22 @@ private async Task WebConfigExpandsVariables(HostingModel hostingModel)
126126
deploymentParameters.WebConfigBasedEnvironmentVariables["OtherVariable"] = "%TestVariable%;Hello";
127127
Assert.Equal("World;Hello", await GetStringAsync(deploymentParameters, "/GetEnvironmentVariable?name=OtherVariable"));
128128
}
129+
130+
[ConditionalTheory]
131+
[RequiresIIS(IISCapability.PoolEnvironmentVariables)]
132+
[RequiresNewHandler]
133+
[RequiresNewShim]
134+
[InlineData(HostingModel.InProcess)]
135+
[InlineData(HostingModel.OutOfProcess)]
136+
public async Task PreferEnvironmentVariablesOverWebConfigWhenConfigured(HostingModel hostingModel)
137+
{
138+
var deploymentParameters = Fixture.GetBaseDeploymentParameters(hostingModel);
139+
140+
var environment = "Development";
141+
deploymentParameters.EnvironmentVariables["ANCM_PREFER_ENVIRONMENT_VARIABLES"] = "true";
142+
deploymentParameters.EnvironmentVariables["ASPNETCORE_ENVIRONMENT"] = environment;
143+
deploymentParameters.WebConfigBasedEnvironmentVariables.Add("ASPNETCORE_ENVIRONMENT", "Debug");
144+
Assert.Equal(environment, await GetStringAsync(deploymentParameters, "/GetEnvironmentVariable?name=ASPNETCORE_ENVIRONMENT"));
145+
}
129146
}
130147
}

src/Servers/IIS/IIS/test/IIS.Shared.FunctionalTests/RequiresIISAttribute.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ static RequiresIISAttribute()
6161

6262
var ancmConfigPath = Path.Combine(Environment.SystemDirectory, "inetsrv", "config", "schema", "aspnetcore_schema.xml");
6363

64+
if (!File.Exists(ancmConfigPath))
65+
{
66+
ancmConfigPath = Path.Combine(Environment.SystemDirectory, "inetsrv", "config", "schema", "aspnetcore_schema_v2.xml");
67+
}
68+
6469
if (!File.Exists(ancmConfigPath) && !SkipInVSTSAttribute.RunningInVSTS)
6570
{
6671
_skipReasonStatic = "IIS Schema is not installed.";

0 commit comments

Comments
 (0)