When deploying an ASP.NET Core application to IIS, you may find IIS returning HTTP Error 502.5 instead of your web page.
I recently hit this problem after manually modifying the web.config
file. Fortunately, the problem is easy to fix.
In this post, we see two different causes of this error, two different solutions for the first cause, a solution for the second cause, and learn what needs to be in web.config
for ASP.NET Core to operate.
The Error
Here is a screenshot of the HTTP Error 502.5 - Process Failure
error. You'll see this, in your browser, when making a request to your ASP.NET Core application, after deployment, if you have this issue.
Why do I get the error?
The HTTP Error 502.5 - Bad Gateway
and HTTP Error 502.5 - Process Failure
error messages occur in ASP.NET Core when IIS fails to execute the dotnet
process.
I've seen this error happen for two different reasons:
- .NET Core Runtime is not installed
web.config
file has not been transformed
Fix 1a: Install the .NET Core Runtime
The most common reason for this to occur is when you haven't installed the .NET Core runtime on the server.
You can download the latest .NET Core runtime from Microsoft's .NET download page.
For Windows, you'll usually want the latest .NET Core runtime (currently v2.1.1), as highlighted in the following screenshot:
This will get you the Windows Hosting Bundle Installer, which will install both the x86 and x64 runtimes on Windows Server.
Fix 1b: Publish a Self-Contained Deployment
If you don't want to install the .NET Core Runtime. An alternative for .NET Core web applications is to publish them in the Self-Contained
deployment mode, which includes the required .NET Runtime files alongside your application.
You can select this option from the advanced publish settings screen in Visual Studio:
If you go with this option, you'll also need to choose a target runtime: win-x86, win-x64, osx-x64, or linux-x64. Because self-contained deployments are not portable.
Fix 2: Transform your web.config file
Another reason for this error to occur is when you deploy an untransformed web.config
file.
This is likely to be your issue if you had a previously working web application and merely deployed a new version of it.
ASP.NET Core Web Config
In ASP.NET Core applications, the web.config
file contains a handler that directs requests to the AspNetCoreModule
and an aspNetCore
element that defines and configures the ASP.NET Core process to execute your web application.
Here is a minimal web.config
file for an ASP.NET Core application:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="true"
stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false"/>
</system.webServer>
</configuration>
Note that it's possible that you don't have a web.config
file in your ASP.NET Core project. If you do not, one will be generated for you when publishing the project, as IIS and IIS Express require a web.config
file when hosting an ASP.NET Core web app.
The Issue
The untransformed web.config
contains the variables %LAUNCHER_PATH%
and %LAUNCHER_ARGS%
rather than the correct paths. When IIS tries to run ASP.NET Core, it uses %LAUNCHER_PATH%
and %LAUNCHER_ARGS%
rather than the correct path and arguments.
To fix the HTTP Error 502.5 in ASP.NET Core, you need to transform the web.config
and replace the untransformed web.config
file on the IIS web server.
How do I transform web.config?
This transformation takes place when you choose to publish
your web application. The transformed web.config
ends up in the published output folder. Therefore, you simply need to publish
your web application and copy the resulting web.config
file onto the server.
In a transformed web.config
file, the aspNetCore
element will look something like this:
<aspNetCore processPath="dotnet" arguments=".\MyApplication.dll" stdoutLogEnabled="true"
stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false" />
%LAUNCHER_PATH%
has been replaced by dotnet
and %LAUNCHER_ARGS%
has been replaced by the path to the main web application dll .\MyApplication.dll
.
Addendum
Update (4th July 2018): The same error occurs when the .NET Core Runtime is not installed. So, I have updated the article to include the solution to this more common issue as well.