500.19 error or 502.5 error when hosting asp.net core 2 application inside IIS

While deploying asp.net core 2 to IIS, you may get one of these errors  or both  :)

500.19 Internal Server error

The first of the issue is 500.19 error when trying to host an asp.net core 2.0 application inside IIS7. What you get might be  something like below

 

This one was quite simple,turned out to be ANCM (ASP.NET CORE MODULE) module and related configuration was not correctly recognized by IIS. This can be fixed by installing the .NET Core Windows Server Hosting bundle  .

By installing the .NET CORE Server Hosting bundle ,it added the ANCM module correctly to IIS .When you install the ANCM module,,it will copy dlls and schema files to as follows

 C:\Windows\System32\inetsrv\aspnetcore.dll

C:\Windows\System32\inetsrv\config\schema\aspnetcore_schema.xml

If you are running IIS application pool on 32 bit ,you should also see it under

 C:\Windows\SysWOW64\inetsrv\aspnetcore.dll.

It will also add the required configuration changes to ApplicationHost.Config(C:\Windows\System32\inetsrv\config) file.You should see AspNetCoreModule in the globalModules section.

  <add name="AspNetCoreModule" image="%SystemRoot%\system32\inetsrv\aspnetcore.dll" />

HTTP Error 502.5 - Process Failure

This error will be like this

aspnet core 502.5 error

If the ASP.NET Core Module fails to start, a 502.5 Process Failure status code page appears. This can be caused by multiple reasons

  • ASP.NET Core Module fails to launch the backend process - This could be because an exception occured in your asp.net core startup.cs 
  • The backend .NET Core process starts but fails to listen on the configured port - You can refer my other blog entry for a similar issue

Please note that if you have disableStartUpErrorPage  setting enabled, you will just get regular 502 error page of IIS and not this specific page 

You web.config will be

  <system.webServer>
 <handlers>
 <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
 </handlers> 
 <aspNetCore processPath="dotnet" arguments=".\web.api.dll" stdoutLogEnabled="true" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="true" />
 </system.webServer>

Step 1 : Look for std log files with failure or enable developer exception page

stdoutLogEnabled should be enabled and look for a log files in the stdoutLogFile location you specified in your web.config file. If this folder is not there,do create the folder and reproduce the issue .

The Logs should appear like

 D:\WebSite\logs>dir
03/13/2018 02:41 PM <DIR> .
03/13/2018 02:41 PM <DIR> ..
03/13/2018 02:40 PM 34,421 stdout_22056_201831391033.log
03/13/2018 02:41 PM 126,180 stdout_22056_201831391038.log
03/13/2018 03:47 PM 1,165,245 stdout_22056_201831391121.log

 

If you do not want to look into the log file by login into the server or does not have access to server 

you can just add ASPNETCORE_ENVIRONMENT environment variable and also enable developer exception page in your startup.cs to see the exception right in browser

 
<aspNetCore processPath="dotnet" arguments=".\MyApp.dll" stdoutLogEnabled="false" stdoutLogFile="\\?\%home%\LogFiles\stdout"> 
<environmentVariables>
 <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" /> 
</environmentVariables> 
</aspNetCore>

and the startup.cs Configure method should have

 public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
 {
if (env.IsDevelopment())
 {
 app.UseDeveloperExceptionPage();
 }

After this,you should see the developer exception page in your application which should tell you what is the error your application is running into.

Step 2 : if step 1 did not help, check if dotnet.exe can be successfully launched  outside IIS

Although we run a single application with asp.net core inside IIS,you are actually running two web servers behind the screen.

asp.net core module under the hood

As shown in the above image, Say you have IIS website running on www.example.com which will be listening on port 80. and by default dotnet.exe launches on port 5000. So from IIS website ANCM will proxy the request to dotnet.exe process.So the failure is basically dotnet.exe process is running or IIS could not launch the process. So  we can easily test to verify if the dotnet.exe process is launching without IIS

  • Open command prompt as Administrator
  • In command prompt, go to the location where you have published the files and run  dotnet <web api dll name>
 D:\WebSite>dotnet web.api.dll
info: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[0]
 User profile is available. Using 'C:\Users\rkolakka102716\AppData\Local\AS
P.NET\DataProtection-Keys' as key repository and Windows DPAPI to encrypt keys a
t rest.
Hosting environment: Production
Content root path: D:\WebSite
Now listening on: https://localhost:5000
Application started. Press Ctrl+C to shut down.

If you see this message "Application started" ,you can see that your application is working fine outside.But if you get any exception here ,that;s what you are after.

Please note that it is important to run the dotnet.exe <web.ap.dll> as administrator.Otherwise you may see an exception like Unable to Bind URL https://localhost:5000

If you are able to run the dotnet.exe from commandline and browse to the website fine,it means you can run the website successfully outside IIS.

Step 3: Check Why IIS is not able to launch the dotnet.exe process

Make Sure Load user Profile setting is true for the application pool identity

set load user profile true for applicationpoolidentity

 

If you have not set this setting,application pool identity  which is running the w3wp.exe  was not able to find the dotnet.exe process.

Hope it helps!