Skip to content

Commit a67b751

Browse files
Improve IIS error when wrong application path (dotnet#46249)
1 parent 888c71f commit a67b751

File tree

5 files changed

+62
-5
lines changed

5 files changed

+62
-5
lines changed

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -321,10 +321,21 @@ try
321321
errorContext.generalErrorType = "Failed to load ASP.NET Core runtime";
322322
errorContext.errorReason = "The specified version of Microsoft.NetCore.App or Microsoft.AspNetCore.App was not found.";
323323

324-
EventLog::Error(
325-
ASPNETCORE_EVENT_GENERAL_ERROR,
326-
ASPNETCORE_EVENT_HOSTFXR_FAILURE_MSG
327-
);
324+
if (intHostFxrExitCode == AppArgNotRunnable)
325+
{
326+
errorContext.detailedErrorContent = "Provided application path does not exist, or isn't a .dll or .exe.";
327+
EventLog::Error(
328+
ASPNETCORE_EVENT_GENERAL_ERROR,
329+
ASPNETCORE_EVENT_HOSTFXR_BAD_APPLICATION_FAILURE_MSG
330+
);
331+
}
332+
else
333+
{
334+
EventLog::Error(
335+
ASPNETCORE_EVENT_GENERAL_ERROR,
336+
ASPNETCORE_EVENT_HOSTFXR_FAILURE_MSG
337+
);
338+
}
328339

329340
return E_UNEXPECTED;
330341
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ typedef int(*hostfxr_get_runtime_property_value_fn)(void* host_context_handle, P
3030
typedef int(*hostfxr_run_app_fn)(void* host_context_handle);
3131
typedef int(*hostfxr_close_fn)(void* hostfxr_context_handle);
3232

33+
const int AppArgNotRunnable = 0x80008094;
34+
3335
class HostFxrErrorRedirector: NonCopyable
3436
{
3537
public:

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#define ASPNETCORE_EVENT_HOSTFXR_DLL_INVALID_VERSION_MSG L"Hostfxr version used does not support 'hostfxr_get_native_search_directories', update the version of hostfxr to a higher version. Path to hostfxr: '%s'."
4242
#define ASPNETCORE_EVENT_HOSTFXR_DLL_UNABLE_TO_LOAD_MSG L"Unable to load '%s'. This might be caused by a bitness mismatch between IIS application pool and published application."
4343
#define ASPNETCORE_EVENT_HOSTFXR_FAILURE_MSG L"Unable to locate application dependencies. Ensure that the versions of Microsoft.NetCore.App and Microsoft.AspNetCore.App targeted by the application are installed."
44+
#define ASPNETCORE_EVENT_HOSTFXR_BAD_APPLICATION_FAILURE_MSG L"Provided application path does not exist, or isn't a .dll or .exe."
4445
#define ASPNETCORE_EVENT_INPROCESS_THREAD_EXCEPTION_MSG L"Application '%s' with physical root '%s' hit unexpected managed exception, exception code = '0x%x'. Please check the stderr logs for more information."
4546
#define ASPNETCORE_EVENT_INPROCESS_THREAD_EXCEPTION_STDOUT_MSG L"Application '%s' with physical root '%s' hit unexpected managed exception, exception code = '0x%x'. First 30KB characters of captured stdout and stderr logs:\r\n%s"
4647
#define ASPNETCORE_EVENT_INPROCESS_RH_ERROR_MSG L"Could not find 'aspnetcorev2_inprocess.dll'. Exception message:\r\n%s"

src/Servers/IIS/IIS/test/Common.FunctionalTests/Infrastructure/EventLogHelpers.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,11 @@ public static string InProcessFailedToFindNativeDependencies(IISDeploymentResult
262262
}
263263
}
264264

265+
public static string InProcessFailedToFindApplication()
266+
{
267+
return "Provided application path does not exist, or isn't a .dll or .exe.";
268+
}
269+
265270
public static string InProcessFailedToFindRequestHandler(IISDeploymentResult deploymentResult)
266271
{
267272
if (DeployerSelector.HasNewShim)

src/Servers/IIS/IIS/test/Common.LongTests/StartupTests.cs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ public async Task RemoveHostfxrFromApp_InProcessHostfxrLoadFailure()
357357
}
358358

359359
[ConditionalFact]
360-
public async Task TargedDifferenceSharedFramework_FailedToFindNativeDependencies()
360+
public async Task TargetDifferenceSharedFramework_FailedToFindNativeDependencies()
361361
{
362362
var deploymentParameters = Fixture.GetBaseDeploymentParameters(Fixture.InProcessTestSite);
363363
var deploymentResult = await DeployAsync(deploymentParameters);
@@ -375,6 +375,25 @@ public async Task TargedDifferenceSharedFramework_FailedToFindNativeDependencies
375375
EventLogHelpers.VerifyEventLogEvent(deploymentResult, EventLogHelpers.InProcessFailedToFindNativeDependencies(deploymentResult), Logger);
376376
}
377377

378+
[ConditionalFact]
379+
[RequiresNewShim]
380+
public async Task WrongApplicationPath_FailedToRun()
381+
{
382+
var deploymentParameters = Fixture.GetBaseDeploymentParameters(Fixture.InProcessTestSite);
383+
deploymentParameters.WebConfigBasedEnvironmentVariables["ASPNETCORE_DETAILEDERRORS"] = "TRUE";
384+
var deploymentResult = await DeployAsync(deploymentParameters);
385+
386+
deploymentResult.ModifyWebConfig(element => element
387+
.Descendants("system.webServer")
388+
.Single()
389+
.GetOrAdd("aspNetCore")
390+
.SetAttributeValue("arguments", "not-exist.dll"));
391+
392+
await AssertSiteFailsToStartWithInProcessStaticContent(deploymentResult, "500.31", "Provided application path does not exist, or isn't a .dll or .exe.");
393+
394+
EventLogHelpers.VerifyEventLogEvent(deploymentResult, EventLogHelpers.InProcessFailedToFindApplication(), Logger);
395+
}
396+
378397
[ConditionalFact]
379398
public async Task SingleExecutable_FailedToFindNativeDependencies()
380399
{
@@ -1559,4 +1578,23 @@ private async Task AssertSiteFailsToStartWithInProcessStaticContent(IISDeploymen
15591578
Assert.Contains(error, await response.Content.ReadAsStringAsync());
15601579
StopServer();
15611580
}
1581+
1582+
private async Task AssertSiteFailsToStartWithInProcessStaticContent(IISDeploymentResult deploymentResult, params string[] errors)
1583+
{
1584+
HttpResponseMessage response = null;
1585+
1586+
// Make sure strings aren't freed.
1587+
for (var i = 0; i < 2; i++)
1588+
{
1589+
response = await deploymentResult.HttpClient.GetAsync("/HelloWorld");
1590+
}
1591+
1592+
Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
1593+
var responseText = await response.Content.ReadAsStringAsync();
1594+
foreach (var error in errors)
1595+
{
1596+
Assert.Contains(error, responseText);
1597+
}
1598+
StopServer();
1599+
}
15621600
}

0 commit comments

Comments
 (0)