From 65d3787fc483959166d9ecc75d392dcee7e90a2a Mon Sep 17 00:00:00 2001 From: Pavel Krymets Date: Fri, 29 Jun 2018 12:42:00 -0700 Subject: [PATCH] Pass parameters to createapplication method (#998) --- .../AspNetCore/Inc/applicationinfo.h | 8 +++++--- .../AspNetCore/src/applicationinfo.cpp | 13 +++++++++++-- .../CommonLib/application.h | 10 ---------- .../CommonLib/iapplication.h | 12 ++++++------ .../InProcessRequestHandler/dllmain.cxx | 16 +++++++++------- .../inprocessapplication.cpp | 17 ++++++++++++++--- .../inprocessapplication.h | 19 ++++--------------- .../OutOfProcessRequestHandler/dllmain.cxx | 4 ++++ .../inprocess_application_tests.cpp | 13 ++++++++----- .../Inprocess/StartupTests.cs | 4 ++++ 10 files changed, 65 insertions(+), 51 deletions(-) diff --git a/src/AspNetCoreModuleV2/AspNetCore/Inc/applicationinfo.h b/src/AspNetCoreModuleV2/AspNetCore/Inc/applicationinfo.h index 3d9073d013..f6f5890b8e 100644 --- a/src/AspNetCoreModuleV2/AspNetCore/Inc/applicationinfo.h +++ b/src/AspNetCoreModuleV2/AspNetCore/Inc/applicationinfo.h @@ -20,9 +20,11 @@ typedef HRESULT (WINAPI * PFN_ASPNETCORE_CREATE_APPLICATION)( - _In_ IHttpServer *pServer, - _In_ IHttpApplication *pHttpApplication, - _Out_ IAPPLICATION **pApplication + _In_ IHttpServer *pServer, + _In_ IHttpApplication *pHttpApplication, + _In_ APPLICATION_PARAMETER *pParameters, + _In_ DWORD nParameters, + _Out_ IAPPLICATION **pApplication ); extern BOOL g_fRecycleProcessCalled; diff --git a/src/AspNetCoreModuleV2/AspNetCore/src/applicationinfo.cpp b/src/AspNetCoreModuleV2/AspNetCore/src/applicationinfo.cpp index eca9c56448..ccbd586f3e 100644 --- a/src/AspNetCoreModuleV2/AspNetCore/src/applicationinfo.cpp +++ b/src/AspNetCoreModuleV2/AspNetCore/src/applicationinfo.cpp @@ -3,6 +3,7 @@ #include "applicationinfo.h" +#include #include "proxymodule.h" #include "hostfxr_utility.h" #include "utility.h" @@ -222,8 +223,16 @@ APPLICATION_INFO::EnsureApplicationCreated( FINISHED(HRESULT_FROM_WIN32(ERROR_INVALID_FUNCTION)); } - FINISHED_IF_FAILED(m_pfnAspNetCoreCreateApplication(m_pServer, pHttpContext->GetApplication(), &pApplication)); - pApplication->SetParameter(L"InProcessExeLocation", struExeLocation.QueryStr()); + std::array parameters { + {"InProcessExeLocation", struExeLocation.QueryStr()} + }; + + FINISHED_IF_FAILED(m_pfnAspNetCoreCreateApplication( + m_pServer, + pHttpContext->GetApplication(), + parameters.data(), + static_cast(parameters.size()), + &pApplication)); m_pApplication = pApplication; } diff --git a/src/AspNetCoreModuleV2/CommonLib/application.h b/src/AspNetCoreModuleV2/CommonLib/application.h index 6025fdd0fb..0c0752f0c4 100644 --- a/src/AspNetCoreModuleV2/CommonLib/application.h +++ b/src/AspNetCoreModuleV2/CommonLib/application.h @@ -40,16 +40,6 @@ public: } } - VOID - SetParameter( - _In_ LPCWSTR pzName, - _In_ LPCWSTR pzValue) - override - { - UNREFERENCED_PARAMETER(pzName); - UNREFERENCED_PARAMETER(pzValue); - } - protected: volatile APPLICATION_STATUS m_status = APPLICATION_STATUS::UNKNOWN; diff --git a/src/AspNetCoreModuleV2/CommonLib/iapplication.h b/src/AspNetCoreModuleV2/CommonLib/iapplication.h index f74ebc021c..dcec7a5e3e 100644 --- a/src/AspNetCoreModuleV2/CommonLib/iapplication.h +++ b/src/AspNetCoreModuleV2/CommonLib/iapplication.h @@ -14,6 +14,12 @@ enum APPLICATION_STATUS FAIL }; +struct APPLICATION_PARAMETER +{ + LPCSTR pzName; + PVOID pValue; +}; + class IAPPLICATION { public: @@ -46,10 +52,4 @@ public: CreateHandler( _In_ IHttpContext *pHttpContext, _Out_ IREQUEST_HANDLER **pRequestHandler) = 0; - - virtual - VOID - SetParameter( - _In_ LPCWSTR pzName, - _In_ LPCWSTR pzValue) = 0; }; diff --git a/src/AspNetCoreModuleV2/InProcessRequestHandler/dllmain.cxx b/src/AspNetCoreModuleV2/InProcessRequestHandler/dllmain.cxx index d0775e1ae7..20314d34a4 100644 --- a/src/AspNetCoreModuleV2/InProcessRequestHandler/dllmain.cxx +++ b/src/AspNetCoreModuleV2/InProcessRequestHandler/dllmain.cxx @@ -82,9 +82,11 @@ BOOL APIENTRY DllMain(HMODULE hModule, HRESULT __stdcall CreateApplication( - _In_ IHttpServer *pServer, - _In_ IHttpApplication *pHttpApplication, - _Out_ IAPPLICATION **ppApplication + _In_ IHttpServer *pServer, + _In_ IHttpApplication *pHttpApplication, + _In_ APPLICATION_PARAMETER *pParameters, + _In_ DWORD nParameters, + _Out_ IAPPLICATION **ppApplication ) { REQUESTHANDLER_CONFIG *pConfig = NULL; @@ -97,11 +99,11 @@ CreateApplication( auto config = std::unique_ptr(pConfig); - BOOL disableStartupPage = pConfig->QueryDisableStartUpErrorPage(); + const bool disableStartupPage = pConfig->QueryDisableStartUpErrorPage(); - auto pApplication = std::make_unique(pServer, std::move(config)); - - if (FAILED(pApplication->LoadManagedApplication())) + auto pApplication = std::make_unique(pServer, std::move(config), pParameters, nParameters); + + if (FAILED_LOG(pApplication->LoadManagedApplication())) { // Set the currently running application to a fake application that returns startup exceptions. *ppApplication = new StartupExceptionApplication(pServer, disableStartupPage); diff --git a/src/AspNetCoreModuleV2/InProcessRequestHandler/inprocessapplication.cpp b/src/AspNetCoreModuleV2/InProcessRequestHandler/inprocessapplication.cpp index 85897d906c..a1cf301054 100644 --- a/src/AspNetCoreModuleV2/InProcessRequestHandler/inprocessapplication.cpp +++ b/src/AspNetCoreModuleV2/InProcessRequestHandler/inprocessapplication.cpp @@ -13,18 +13,22 @@ #include "exceptions.h" #include "LoggingHelpers.h" +const LPCSTR IN_PROCESS_APPLICATION::s_exeLocationParameterName = "InProcessExeLocation"; + IN_PROCESS_APPLICATION* IN_PROCESS_APPLICATION::s_Application = NULL; IN_PROCESS_APPLICATION::IN_PROCESS_APPLICATION( IHttpServer *pHttpServer, - std::unique_ptr pConfig) : + std::unique_ptr pConfig, + APPLICATION_PARAMETER *pParameters, + DWORD nParameters) : + InProcessApplicationBase(pHttpServer), m_pHttpServer(pHttpServer), m_ProcessExitCode(0), m_fBlockCallbacksIntoManaged(FALSE), - m_fInitialized(FALSE), m_fShutdownCalledFromNative(FALSE), m_fShutdownCalledFromManaged(FALSE), - InProcessApplicationBase(pHttpServer), + m_fInitialized(FALSE), m_pConfig(std::move(pConfig)) { // is it guaranteed that we have already checked app offline at this point? @@ -32,6 +36,13 @@ IN_PROCESS_APPLICATION::IN_PROCESS_APPLICATION( DBG_ASSERT(pHttpServer != NULL); DBG_ASSERT(pConfig != NULL); + for (DWORD i = 0; i < nParameters; i++) + { + if (_stricmp(pParameters[i].pzName, s_exeLocationParameterName) == 0) + { + m_struExeLocation.Copy(reinterpret_cast(pParameters[i].pValue)); + } + } m_status = APPLICATION_STATUS::STARTING; } diff --git a/src/AspNetCoreModuleV2/InProcessRequestHandler/inprocessapplication.h b/src/AspNetCoreModuleV2/InProcessRequestHandler/inprocessapplication.h index 6b3bfd18a0..034c36fc93 100644 --- a/src/AspNetCoreModuleV2/InProcessRequestHandler/inprocessapplication.h +++ b/src/AspNetCoreModuleV2/InProcessRequestHandler/inprocessapplication.h @@ -18,7 +18,9 @@ class IN_PROCESS_APPLICATION : public InProcessApplicationBase public: IN_PROCESS_APPLICATION( IHttpServer* pHttpServer, - std::unique_ptr pConfig); + std::unique_ptr pConfig, + APPLICATION_PARAMETER *pParameters, + DWORD nParameters); ~IN_PROCESS_APPLICATION(); @@ -42,19 +44,6 @@ public: _Out_ IREQUEST_HANDLER **pRequestHandler) override; - VOID - SetParameter( - _In_ LPCWSTR pzName, - _In_ LPCWSTR pzValue) - override - { - const auto exeLocationParameterName = L"InProcessExeLocation"; - if (_wcsicmp(pzName, exeLocationParameterName) == 0) - { - m_struExeLocation.Copy(pzValue); - } - } - // Executes the .NET Core process HRESULT ExecuteApplication( @@ -162,7 +151,7 @@ private: IOutputManager* m_pLoggerProvider; std::unique_ptr m_pConfig; - + static const LPCSTR s_exeLocationParameterName; static VOID diff --git a/src/AspNetCoreModuleV2/OutOfProcessRequestHandler/dllmain.cxx b/src/AspNetCoreModuleV2/OutOfProcessRequestHandler/dllmain.cxx index 0a416a4c7b..c74f3cacaa 100644 --- a/src/AspNetCoreModuleV2/OutOfProcessRequestHandler/dllmain.cxx +++ b/src/AspNetCoreModuleV2/OutOfProcessRequestHandler/dllmain.cxx @@ -272,9 +272,13 @@ __stdcall CreateApplication( _In_ IHttpServer *pServer, _In_ IHttpApplication *pHttpApplication, + _In_ APPLICATION_PARAMETER *pParameters, + _In_ DWORD nParameters, _Out_ IAPPLICATION **ppApplication ) { + UNREFERENCED_PARAMETER(pParameters); + UNREFERENCED_PARAMETER(nParameters); HRESULT hr = S_OK; IAPPLICATION *pApplication = NULL; REQUESTHANDLER_CONFIG *pConfig = NULL; diff --git a/test/CommonLibTests/inprocess_application_tests.cpp b/test/CommonLibTests/inprocess_application_tests.cpp index d362b8f56c..3f1f67e457 100644 --- a/test/CommonLibTests/inprocess_application_tests.cpp +++ b/test/CommonLibTests/inprocess_application_tests.cpp @@ -3,6 +3,7 @@ #include "stdafx.h" +#include #include "inprocessapplication.h" #include "fakeclasses.h" @@ -17,11 +18,13 @@ namespace InprocessTests auto server = new MockHttpServer(); auto requestHandlerConfig = MockRequestHandlerConfig::CreateConfig(); auto config = std::unique_ptr(requestHandlerConfig); - IN_PROCESS_APPLICATION *app = new IN_PROCESS_APPLICATION(server, std::move(config)); - { - std::wstring exePath(L"hello"); - app->SetParameter(L"InProcessExeLocation", exePath.c_str()); - } + + std::wstring exePath(L"hello"); + std::array parameters { + {"InProcessExeLocation", exePath.data()} + }; + + IN_PROCESS_APPLICATION *app = new IN_PROCESS_APPLICATION(server, std::move(config), parameters.data(), 1); ASSERT_STREQ(app->QueryExeLocation(), L"hello"); } } diff --git a/test/IISIntegration.FunctionalTests/Inprocess/StartupTests.cs b/test/IISIntegration.FunctionalTests/Inprocess/StartupTests.cs index f848ab910a..051ae6a2dc 100644 --- a/test/IISIntegration.FunctionalTests/Inprocess/StartupTests.cs +++ b/test/IISIntegration.FunctionalTests/Inprocess/StartupTests.cs @@ -3,6 +3,7 @@ using System; using System.IO; +using System.Linq; using System.Net; using System.Threading.Tasks; using IISIntegration.FunctionalTests.Utilities; @@ -77,6 +78,9 @@ namespace Microsoft.AspNetCore.Server.IISIntegration.FunctionalTests await AssertStarts( deploymentResult => Helpers.ModifyAspNetCoreSectionInWebConfig(deploymentResult, "processPath", path), deploymentParameters => deploymentParameters.EnvironmentVariables["PATH"] = Path.GetDirectoryName(_dotnetLocation)); + + // Verify that in this scenario where.exe was invoked only once by shim and request handler uses cached value + Assert.Equal(1, TestSink.Writes.Count(w => w.Message.Contains("Invoking where.exe to find dotnet.exe"))); } private async Task AssertStarts(Action postDeploy, Action preDeploy = null)