[ANCM] Add switch to prefer env over web.config (#19746)

This commit is contained in:
Brennan 2020-03-11 15:53:41 -07:00 committed by GitHub
parent 56a64a6ff0
commit fdaa334567
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 42 additions and 1 deletions

View File

@ -13,6 +13,7 @@
#define ASPNETCORE_IIS_AUTH_BASIC L"basic;"
#define ASPNETCORE_IIS_AUTH_ANONYMOUS L"anonymous;"
#define ASPNETCORE_IIS_AUTH_NONE L"none"
#define ANCM_PREFER_ENVIRONMENT_VARIABLES_ENV_STR L"ANCM_PREFER_ENVIRONMENT_VARIABLES"
//
// The key used for hash-table lookups, consists of the port on which the http process is created.

View File

@ -78,9 +78,27 @@ public:
environmentVariables.insert_or_assign(HOSTING_STARTUP_ASSEMBLIES_ENV_STR, hostingStartupValues);
}
auto preferEnvironmentVariablesSetting = Environment::GetEnvironmentVariableValue(ANCM_PREFER_ENVIRONMENT_VARIABLES_ENV_STR).value_or(L"false");
auto preferEnvironmentVariables = equals_ignore_case(L"1", preferEnvironmentVariablesSetting) || equals_ignore_case(L"true", preferEnvironmentVariablesSetting);
for (auto& environmentVariable : environmentVariables)
{
environmentVariable.second = Environment::ExpandEnvironmentVariables(environmentVariable.second);
if (preferEnvironmentVariables)
{
auto env = Environment::GetEnvironmentVariableValue(environmentVariable.first);
if (env.has_value())
{
environmentVariable.second = env.value();
}
else
{
environmentVariable.second = Environment::ExpandEnvironmentVariables(environmentVariable.second);
}
}
else
{
environmentVariable.second = Environment::ExpandEnvironmentVariables(environmentVariable.second);
}
}
return environmentVariables;

View File

@ -126,5 +126,22 @@ namespace Microsoft.AspNetCore.Server.IIS.FunctionalTests.InProcess
deploymentParameters.WebConfigBasedEnvironmentVariables["OtherVariable"] = "%TestVariable%;Hello";
Assert.Equal("World;Hello", await GetStringAsync(deploymentParameters, "/GetEnvironmentVariable?name=OtherVariable"));
}
[ConditionalTheory]
[RequiresIIS(IISCapability.PoolEnvironmentVariables)]
[RequiresNewHandler]
[RequiresNewShim]
[InlineData(HostingModel.InProcess)]
[InlineData(HostingModel.OutOfProcess)]
public async Task PreferEnvironmentVariablesOverWebConfigWhenConfigured(HostingModel hostingModel)
{
var deploymentParameters = Fixture.GetBaseDeploymentParameters(hostingModel);
var environment = "Development";
deploymentParameters.EnvironmentVariables["ANCM_PREFER_ENVIRONMENT_VARIABLES"] = "true";
deploymentParameters.EnvironmentVariables["ASPNETCORE_ENVIRONMENT"] = environment;
deploymentParameters.WebConfigBasedEnvironmentVariables.Add("ASPNETCORE_ENVIRONMENT", "Debug");
Assert.Equal(environment, await GetStringAsync(deploymentParameters, "/GetEnvironmentVariable?name=ASPNETCORE_ENVIRONMENT"));
}
}
}

View File

@ -61,6 +61,11 @@ namespace Microsoft.AspNetCore.Server.IIS.FunctionalTests
var ancmConfigPath = Path.Combine(Environment.SystemDirectory, "inetsrv", "config", "schema", "aspnetcore_schema.xml");
if (!File.Exists(ancmConfigPath))
{
ancmConfigPath = Path.Combine(Environment.SystemDirectory, "inetsrv", "config", "schema", "aspnetcore_schema_v2.xml");
}
if (!File.Exists(ancmConfigPath) && !SkipInVSTSAttribute.RunningInVSTS)
{
_skipReasonStatic = "IIS Schema is not installed.";