Skip to content

Commit 6ddbfb6

Browse files
authored
Improve configuration file handling logic (#1317)
1 parent 2cbf5bd commit 6ddbfb6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+877
-239
lines changed

src/AspNetCoreModuleV2/AspNetCore/AspNetCore.vcxproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@
231231
<ClInclude Include="applicationinfo.h" />
232232
<ClInclude Include="AppOfflineApplication.h" />
233233
<ClInclude Include="AppOfflineHandler.h" />
234-
<ClInclude Include="aspnetcore_shim_config.h" />
234+
<ClInclude Include="ShimOptions.h" />
235235
<ClInclude Include="globalmodule.h" />
236236
<ClInclude Include="applicationmanager.h" />
237237
<ClInclude Include="HandlerResolver.h" />
@@ -246,7 +246,7 @@
246246
<ClCompile Include="applicationmanager.cpp" />
247247
<ClCompile Include="AppOfflineApplication.cpp" />
248248
<ClCompile Include="AppOfflineHandler.cpp" />
249-
<ClCompile Include="aspnetcore_shim_config.cpp" />
249+
<ClCompile Include="ShimOptions.cpp" />
250250
<ClCompile Include="dllmain.cpp" />
251251
<ClCompile Include="globalmodule.cpp" />
252252
<ClCompile Include="HandlerResolver.cpp" />

src/AspNetCoreModuleV2/AspNetCore/HandlerResolver.cpp

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#include "file_utility.h"
1313
#include "LoggingHelpers.h"
1414
#include "resources.h"
15+
#include "ConfigurationLoadException.h"
16+
#include "WebConfigConfigurationSource.h"
1517

1618
const PCWSTR HandlerResolver::s_pwzAspnetcoreInProcessRequestHandlerName = L"aspnetcorev2_inprocess.dll";
1719
const PCWSTR HandlerResolver::s_pwzAspnetcoreOutOfProcessRequestHandlerName = L"aspnetcorev2_outofprocess.dll";
@@ -25,7 +27,7 @@ HandlerResolver::HandlerResolver(HMODULE hModule, IHttpServer &pServer)
2527
}
2628

2729
HRESULT
28-
HandlerResolver::LoadRequestHandlerAssembly(IHttpApplication &pApplication, ASPNETCORE_SHIM_CONFIG& pConfiguration, std::unique_ptr<ApplicationFactory>& pApplicationFactory)
30+
HandlerResolver::LoadRequestHandlerAssembly(IHttpApplication &pApplication, ShimOptions& pConfiguration, std::unique_ptr<ApplicationFactory>& pApplicationFactory)
2931
{
3032
HRESULT hr;
3133
PCWSTR pstrHandlerDllName;
@@ -65,7 +67,7 @@ HandlerResolver::LoadRequestHandlerAssembly(IHttpApplication &pApplication, ASPN
6567
RETURN_IF_FAILED(LoggingHelpers::CreateLoggingProvider(
6668
pConfiguration.QueryStdoutLogEnabled(),
6769
!m_pServer.IsCommandLineLaunch(),
68-
pConfiguration.QueryStdoutLogFile()->QueryStr(),
70+
pConfiguration.QueryStdoutLogFile().c_str(),
6971
pApplication.GetApplicationPhysicalPath(),
7072
outputManager));
7173

@@ -129,20 +131,20 @@ HandlerResolver::GetApplicationFactory(IHttpApplication &pApplication, std::uniq
129131
{
130132
try
131133
{
132-
ASPNETCORE_SHIM_CONFIG pConfiguration;
133-
RETURN_IF_FAILED(pConfiguration.Populate(&m_pServer, &pApplication));
134+
const WebConfigConfigurationSource configurationSource(m_pServer.GetAdminManager(), pApplication);
135+
ShimOptions options(configurationSource);
134136

135137
SRWExclusiveLock lock(m_requestHandlerLoadLock);
136138
if (m_loadedApplicationHostingModel != HOSTING_UNKNOWN)
137139
{
138140
// Mixed hosting models
139-
if (m_loadedApplicationHostingModel != pConfiguration.QueryHostingModel())
141+
if (m_loadedApplicationHostingModel != options.QueryHostingModel())
140142
{
141143
EventLog::Error(
142144
ASPNETCORE_EVENT_MIXED_HOSTING_MODEL_ERROR,
143145
ASPNETCORE_EVENT_MIXED_HOSTING_MODEL_ERROR_MSG,
144146
pApplication.GetApplicationId(),
145-
pConfiguration.QueryHostingModel());
147+
options.QueryHostingModel());
146148

147149
return E_FAIL;
148150
}
@@ -158,11 +160,20 @@ HandlerResolver::GetApplicationFactory(IHttpApplication &pApplication, std::uniq
158160
}
159161
}
160162

161-
m_loadedApplicationHostingModel = pConfiguration.QueryHostingModel();
163+
m_loadedApplicationHostingModel = options.QueryHostingModel();
162164
m_loadedApplicationId = pApplication.GetApplicationId();
163-
RETURN_IF_FAILED(LoadRequestHandlerAssembly(pApplication, pConfiguration, pApplicationFactory));
165+
RETURN_IF_FAILED(LoadRequestHandlerAssembly(pApplication, options, pApplicationFactory));
164166

165167
}
168+
catch(ConfigurationLoadException &ex)
169+
{
170+
EventLog::Error(
171+
ASPNETCORE_CONFIGURATION_LOAD_ERROR,
172+
ASPNETCORE_CONFIGURATION_LOAD_ERROR_MSG,
173+
ex.get_message().c_str());
174+
175+
RETURN_HR(E_FAIL);
176+
}
166177
CATCH_RETURN();
167178

168179
return S_OK;
@@ -178,7 +189,7 @@ void HandlerResolver::ResetHostingModel()
178189

179190
HRESULT
180191
HandlerResolver::FindNativeAssemblyFromGlobalLocation(
181-
ASPNETCORE_SHIM_CONFIG& pConfiguration,
192+
ShimOptions& pConfiguration,
182193
PCWSTR pstrHandlerDllName,
183194
std::wstring& handlerDllPath
184195
)

src/AspNetCoreModuleV2/AspNetCore/HandlerResolver.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
// Licensed under the MIT License. See License.txt in the project root for license information.
33

44
#pragma once
5-
#include "aspnetcore_shim_config.h"
65

76
#include <memory>
87
#include <string>
8+
#include "ShimOptions.h"
99
#include "hostfxroptions.h"
1010
#include "HandleWrapper.h"
1111
#include "ApplicationFactory.h"
@@ -18,8 +18,8 @@ class HandlerResolver
1818
void ResetHostingModel();
1919

2020
private:
21-
HRESULT LoadRequestHandlerAssembly(IHttpApplication &pApplication, ASPNETCORE_SHIM_CONFIG& pConfiguration, std::unique_ptr<ApplicationFactory>& pApplicationFactory);
22-
HRESULT FindNativeAssemblyFromGlobalLocation(ASPNETCORE_SHIM_CONFIG& pConfiguration, PCWSTR libraryName, std::wstring& handlerDllPath);
21+
HRESULT LoadRequestHandlerAssembly(IHttpApplication &pApplication, ShimOptions& pConfiguration, std::unique_ptr<ApplicationFactory>& pApplicationFactory);
22+
HRESULT FindNativeAssemblyFromGlobalLocation(ShimOptions& pConfiguration, PCWSTR libraryName, std::wstring& handlerDllPath);
2323
HRESULT FindNativeAssemblyFromHostfxr(HOSTFXR_OPTIONS& hostfxrOptions, PCWSTR libraryName, std::wstring& handlerDllPath);
2424

2525
HMODULE m_hModule;

src/AspNetCoreModuleV2/AspNetCore/PollingAppOfflineApplication.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class PollingAppOfflineApplication: public APPLICATION
1515
{
1616
public:
1717
PollingAppOfflineApplication(IHttpApplication& pApplication, PollingAppOfflineApplicationMode mode)
18-
:
18+
: APPLICATION(pApplication),
1919
m_ulLastCheckTime(0),
2020
m_appOfflineLocation(GetAppOfflineLocation(pApplication)),
2121
m_fAppOfflineFound(false),
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the MIT License. See License.txt in the project root for license information.
3+
4+
#include "ShimOptions.h"
5+
6+
#include "StringHelpers.h"
7+
#include "ConfigurationLoadException.h"
8+
9+
#define CS_ASPNETCORE_HANDLER_VERSION L"handlerVersion"
10+
11+
ShimOptions::ShimOptions(const ConfigurationSource &configurationSource) :
12+
m_hostingModel(HOSTING_UNKNOWN),
13+
m_fStdoutLogEnabled(false)
14+
{
15+
auto const section = configurationSource.GetRequiredSection(CS_ASPNETCORE_SECTION);
16+
auto hostingModel = section->GetString(CS_ASPNETCORE_HOSTING_MODEL).value_or(L"");
17+
18+
if (hostingModel.empty() || equals_ignore_case(hostingModel, CS_ASPNETCORE_HOSTING_MODEL_OUTOFPROCESS))
19+
{
20+
m_hostingModel = HOSTING_OUT_PROCESS;
21+
}
22+
else if (equals_ignore_case(hostingModel, CS_ASPNETCORE_HOSTING_MODEL_INPROCESS))
23+
{
24+
m_hostingModel = HOSTING_IN_PROCESS;
25+
}
26+
else
27+
{
28+
throw ConfigurationLoadException(format(
29+
L"Unknown hosting model '%s'. Please specify either hostingModel=\"inprocess\" "
30+
"or hostingModel=\"outofprocess\" in the web.config file.", hostingModel.c_str()));
31+
}
32+
33+
if (m_hostingModel == HOSTING_OUT_PROCESS)
34+
{
35+
const auto handlerSettings = section->GetKeyValuePairs(CS_ASPNETCORE_HANDLER_SETTINGS);
36+
m_strHandlerVersion = find_element(handlerSettings, CS_ASPNETCORE_HANDLER_VERSION).value_or(std::wstring());
37+
}
38+
39+
m_strProcessPath = section->GetRequiredString(CS_ASPNETCORE_PROCESS_EXE_PATH);
40+
m_strArguments = section->GetString(CS_ASPNETCORE_PROCESS_ARGUMENTS).value_or(CS_ASPNETCORE_PROCESS_ARGUMENTS_DEFAULT);
41+
m_fStdoutLogEnabled = section->GetRequiredBool(CS_ASPNETCORE_STDOUT_LOG_ENABLED);
42+
m_struStdoutLogFile = section->GetRequiredString(CS_ASPNETCORE_STDOUT_LOG_FILE);
43+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the MIT License. See License.txt in the project root for license information.
3+
4+
#pragma once
5+
6+
#include <string>
7+
#include "ConfigurationSource.h"
8+
#include "exceptions.h"
9+
10+
enum APP_HOSTING_MODEL
11+
{
12+
HOSTING_UNKNOWN = 0,
13+
HOSTING_IN_PROCESS,
14+
HOSTING_OUT_PROCESS
15+
};
16+
17+
class ShimOptions: NonCopyable
18+
{
19+
public:
20+
const std::wstring&
21+
QueryProcessPath() const
22+
{
23+
return m_strProcessPath;
24+
}
25+
26+
const std::wstring&
27+
QueryArguments() const
28+
{
29+
return m_strArguments;
30+
}
31+
32+
APP_HOSTING_MODEL
33+
QueryHostingModel() const
34+
{
35+
return m_hostingModel;
36+
}
37+
38+
const std::wstring&
39+
QueryHandlerVersion() const
40+
{
41+
return m_strHandlerVersion;
42+
}
43+
44+
BOOL
45+
QueryStdoutLogEnabled() const
46+
{
47+
return m_fStdoutLogEnabled;
48+
}
49+
50+
const std::wstring&
51+
QueryStdoutLogFile() const
52+
{
53+
return m_struStdoutLogFile;
54+
}
55+
56+
ShimOptions(const ConfigurationSource &configurationSource);
57+
58+
private:
59+
std::wstring m_strArguments;
60+
std::wstring m_strProcessPath;
61+
APP_HOSTING_MODEL m_hostingModel;
62+
std::wstring m_strHandlerVersion;
63+
std::wstring m_struStdoutLogFile;
64+
bool m_fStdoutLogEnabled;
65+
};

src/AspNetCoreModuleV2/AspNetCore/applicationinfo.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#pragma once
55

66
#include "hostfxroptions.h"
7-
#include "aspnetcore_shim_config.h"
87
#include "iapplication.h"
98
#include "SRWSharedLock.h"
109
#include "HandlerResolver.h"

src/AspNetCoreModuleV2/AspNetCore/aspnetcore_shim_config.cpp

Lines changed: 0 additions & 80 deletions
This file was deleted.

0 commit comments

Comments
 (0)