Skip to content

Commit 25b4f80

Browse files
Add environment variables for Launcher path and args in ANCM (#19797)
1 parent 361dd0c commit 25b4f80

File tree

5 files changed

+89
-13
lines changed

5 files changed

+89
-13
lines changed

src/Servers/IIS/AspNetCoreModuleV2/AspNetCore/ShimOptions.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ ShimOptions::ShimOptions(const ConfigurationSource &configurationSource) :
5454
.value_or(environmentVariables[CS_ASPNETCORE_ENVIRONMENT]);
5555
const auto dotnetEnvironment = Environment::GetEnvironmentVariableValue(CS_DOTNET_ENVIRONMENT)
5656
.value_or(environmentVariables[CS_DOTNET_ENVIRONMENT]);
57+
// We prefer the environment variables for LAUNCHER_PATH and LAUNCHER_ARGS
58+
m_strProcessPath = Environment::GetEnvironmentVariableValue(CS_ANCM_LAUNCHER_PATH)
59+
.value_or(m_strProcessPath);
60+
m_strArguments = Environment::GetEnvironmentVariableValue(CS_ANCM_LAUNCHER_ARGS)
61+
.value_or(m_strArguments);
5762

5863
auto detailedErrorsEnabled = equals_ignore_case(L"1", detailedErrors) || equals_ignore_case(L"true", detailedErrors);
5964
auto aspnetCoreEnvironmentEnabled = equals_ignore_case(L"Development", aspnetCoreEnvironment);

src/Servers/IIS/AspNetCoreModuleV2/CommonLib/ConfigurationSection.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
#define CS_ASPNETCORE_DETAILEDERRORS L"ASPNETCORE_DETAILEDERRORS"
3434
#define CS_ASPNETCORE_ENVIRONMENT L"ASPNETCORE_ENVIRONMENT"
3535
#define CS_DOTNET_ENVIRONMENT L"DOTNET_ENVIRONMENT"
36+
#define CS_ANCM_LAUNCHER_PATH L"ANCM_LAUNCHER_PATH"
37+
#define CS_ANCM_LAUNCHER_ARGS L"ANCM_LAUNCHER_ARGS"
3638

3739
class ConfigurationSection: NonCopyable
3840
{

src/Servers/IIS/AspNetCoreModuleV2/InProcessRequestHandler/InProcessOptions.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "InProcessOptions.h"
55
#include "InvalidOperationException.h"
66
#include "EventLog.h"
7+
#include "Environment.h"
78

89
HRESULT InProcessOptions::Create(
910
IHttpServer& pServer,
@@ -51,6 +52,11 @@ InProcessOptions::InProcessOptions(const ConfigurationSource &configurationSourc
5152
auto const aspNetCoreSection = configurationSource.GetRequiredSection(CS_ASPNETCORE_SECTION);
5253
m_strArguments = aspNetCoreSection->GetString(CS_ASPNETCORE_PROCESS_ARGUMENTS).value_or(CS_ASPNETCORE_PROCESS_ARGUMENTS_DEFAULT);
5354
m_strProcessPath = aspNetCoreSection->GetRequiredString(CS_ASPNETCORE_PROCESS_EXE_PATH);
55+
// We prefer the environment variables for LAUNCHER_PATH and LAUNCHER_ARGS
56+
m_strProcessPath = Environment::GetEnvironmentVariableValue(CS_ANCM_LAUNCHER_PATH)
57+
.value_or(m_strProcessPath);
58+
m_strArguments = Environment::GetEnvironmentVariableValue(CS_ANCM_LAUNCHER_ARGS)
59+
.value_or(m_strArguments);
5460
m_fStdoutLogEnabled = aspNetCoreSection->GetRequiredBool(CS_ASPNETCORE_STDOUT_LOG_ENABLED);
5561
m_struStdoutLogFile = aspNetCoreSection->GetRequiredString(CS_ASPNETCORE_STDOUT_LOG_FILE);
5662
m_fDisableStartUpErrorPage = aspNetCoreSection->GetRequiredBool(CS_ASPNETCORE_DISABLE_START_UP_ERROR_PAGE);

src/Servers/IIS/AspNetCoreModuleV2/RequestHandlerLib/requesthandler_config.cpp

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "environmentvariablehash.h"
88
#include "exceptions.h"
99
#include "config_utility.h"
10+
#include "Environment.h"
1011

1112
REQUESTHANDLER_CONFIG::~REQUESTHANDLER_CONFIG()
1213
{
@@ -101,6 +102,8 @@ REQUESTHANDLER_CONFIG::Populate(
101102
BSTR bstrBasicAuthSection = NULL;
102103
BSTR bstrAnonymousAuthSection = NULL;
103104
BSTR bstrAspNetCoreSection = NULL;
105+
std::optional<std::wstring> launcherPathEnv;
106+
std::optional<std::wstring> launcherArgsEnv;
104107

105108
pAdminManager = pHttpServer->GetAdminManager();
106109
try
@@ -248,12 +251,47 @@ REQUESTHANDLER_CONFIG::Populate(
248251
goto Finished;
249252
}
250253

251-
hr = GetElementStringProperty(pAspNetCoreElement,
252-
CS_ASPNETCORE_PROCESS_EXE_PATH,
253-
&m_struProcessPath);
254-
if (FAILED(hr))
254+
// We prefer the environment variables for LAUNCHER_PATH and LAUNCHER_ARGS
255+
try
255256
{
256-
goto Finished;
257+
launcherPathEnv = Environment::GetEnvironmentVariableValue(CS_ANCM_LAUNCHER_PATH);
258+
launcherArgsEnv = Environment::GetEnvironmentVariableValue(CS_ANCM_LAUNCHER_ARGS);
259+
}
260+
catch(...)
261+
{
262+
FINISHED_IF_FAILED(E_FAIL);
263+
}
264+
265+
if (launcherPathEnv.has_value())
266+
{
267+
hr = m_struProcessPath.Copy(launcherPathEnv.value().c_str());
268+
FINISHED_IF_FAILED(hr);
269+
}
270+
else
271+
{
272+
hr = GetElementStringProperty(pAspNetCoreElement,
273+
CS_ASPNETCORE_PROCESS_EXE_PATH,
274+
&m_struProcessPath);
275+
if (FAILED(hr))
276+
{
277+
goto Finished;
278+
}
279+
}
280+
281+
if (launcherArgsEnv.has_value())
282+
{
283+
hr = m_struArguments.Copy(launcherArgsEnv.value().c_str());
284+
FINISHED_IF_FAILED(hr);
285+
}
286+
else
287+
{
288+
hr = GetElementStringProperty(pAspNetCoreElement,
289+
CS_ASPNETCORE_PROCESS_ARGUMENTS,
290+
&m_struArguments);
291+
if (FAILED(hr))
292+
{
293+
goto Finished;
294+
}
257295
}
258296

259297
hr = GetElementStringProperty(pAspNetCoreElement,
@@ -281,14 +319,6 @@ REQUESTHANDLER_CONFIG::Populate(
281319
goto Finished;
282320
}
283321

284-
hr = GetElementStringProperty(pAspNetCoreElement,
285-
CS_ASPNETCORE_PROCESS_ARGUMENTS,
286-
&m_struArguments);
287-
if (FAILED(hr))
288-
{
289-
goto Finished;
290-
}
291-
292322
hr = GetElementDWORDProperty(pAspNetCoreElement,
293323
CS_ASPNETCORE_RAPID_FAILS_PER_MINUTE,
294324
&m_dwRapidFailsPerMinute);

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -880,6 +880,39 @@ public async Task StackOverflowCanBeSetBySettingLargerStackViaHandlerSetting()
880880
Assert.True(result.IsSuccessStatusCode);
881881
}
882882

883+
[ConditionalTheory]
884+
[RequiresIIS(IISCapability.PoolEnvironmentVariables)]
885+
[RequiresNewShim]
886+
[RequiresNewHandler]
887+
[InlineData(HostingModel.InProcess)]
888+
[InlineData(HostingModel.OutOfProcess)]
889+
public async Task EnvironmentVariableForLauncherPathIsPreferred(HostingModel hostingModel)
890+
{
891+
var deploymentParameters = Fixture.GetBaseDeploymentParameters(hostingModel);
892+
893+
deploymentParameters.EnvironmentVariables["ANCM_LAUNCHER_PATH"] = _dotnetLocation;
894+
deploymentParameters.WebConfigActionList.Add(WebConfigHelpers.AddOrModifyAspNetCoreSection("processPath", "nope"));
895+
896+
await StartAsync(deploymentParameters);
897+
}
898+
899+
[ConditionalTheory]
900+
[RequiresIIS(IISCapability.PoolEnvironmentVariables)]
901+
[RequiresNewShim]
902+
[RequiresNewHandler]
903+
[InlineData(HostingModel.InProcess)]
904+
[InlineData(HostingModel.OutOfProcess)]
905+
public async Task EnvironmentVariableForLauncherArgsIsPreferred(HostingModel hostingModel)
906+
{
907+
var deploymentParameters = Fixture.GetBaseDeploymentParameters(hostingModel);
908+
using var publishedApp = await deploymentParameters.ApplicationPublisher.Publish(deploymentParameters, LoggerFactory.CreateLogger("test"));
909+
910+
deploymentParameters.EnvironmentVariables["ANCM_LAUNCHER_ARGS"] = Path.ChangeExtension(Path.Combine(publishedApp.Path, deploymentParameters.ApplicationPublisher.ApplicationPath), ".dll");
911+
deploymentParameters.WebConfigActionList.Add(WebConfigHelpers.AddOrModifyAspNetCoreSection("arguments", "nope"));
912+
913+
await StartAsync(deploymentParameters);
914+
}
915+
883916
private static void VerifyDotnetRuntimeEventLog(IISDeploymentResult deploymentResult)
884917
{
885918
var entries = GetEventLogsFromDotnetRuntime(deploymentResult);

0 commit comments

Comments
 (0)