This article will provide details about how to deploy ASP.NET Core web application or Web API on IIS. .NET Core 2.2 onwards there were significant changes in the hosting model to IIS. Prior to .NET Core 2.2 only option available was to use IIS as a reverse proxy to ASP.NET Core Kestrel web server.
With .NET Core 2.2 option was added for direct in-process hosting to IIS which can improve the performance significantly. The old approach with IIS as a reverse proxy is also available as an out-of-process hosting model. ASP.NET Core hosting on IIS supports two models for hosting on the IIS i.e. In-process hosting model & out-of-process hosting model.
ASP.NET Core Hosting on IIS is different as compared to ASP.NET Webforms or ASP.NET MVC.
Supported operating systems
Windows 7 or later
Windows server 2012 R2 or later
Apps published for 32 bit (x86) and 64 bit (x64) are supported. Application pools under should be configured accordingly for 32-bit & 64-bit apps.
Type of Hosting Models
In-process hosting model
As the name suggests ASP.NET Core application runs in the same process as IIS (w3wp.exe) and it does not use the Kestrel web server. This hosting model uses IISHttpServer that is hosted directly inside the application pool. This hosting model provides a significant performance improvement over the other out-of-process model as it does not have to forward requests to Kestrel for processing. IIS handles process management in the traditional way with the help of windows process activation service (WAS).
Request flow details:
Request arrives from web at HTTP.sys
HTTP.sys routes the request to IIS
ASP.NET Core module receives the request and passes it to IIS HTTP Server.
IISHTTPServer passes the request to ASP.NET Core middleware pipeline.
ASP.NET Core application handles the request and returns response
Out-of-process hosting model
Again as the name suggests here ASP.NET Core application runs in a separate process from the IIS worker process. This is similar to what was available in ASP.NET Core versions prior to 2.2. ASP.NET Core module handles process management, this module is responsible for starting the ASP.NET Core App when the first request arrives & also restarts if it shuts down or crashes.
Here IIS acts as a reverse proxy to Kestrel web server where ASP.NET Core application is running. For application to be configured as an out-of-process model it needs to specify UseIISIntegration as part of WebHostBuilder code in the program.cs file. ASP.NET Core module doesn’t support HTTPS forwarding.
Steps for ASP.NET Core hosting on IIS
Publish your application
Install/Enable IIS Server on windows
Install the .NET Core Hosting Bundle
Create a new application pool & website for your application
Publish your Application
Right click on project in Solution Explorer and select Publish option. This should being up the screen to publish your code. Select Folder option to publish all the files to a folder. This folder will be configured in IIS as website folder.
Use the Add Roles & Features wizard to Enable IIS on Windows server operating system.
Select following services under web server on Select role services screen
For windows desktop operating system select Control Panel ==> Program ==> Program & Features ==> Turn windows features on or off and select following features for installations
Install .NET Hosting Bundle
You can download the required version of the hosting bundle from here. The hosting bundle installs the .NET Core Runtime, .NET Core Library, and the ASP.NET Core Module.
If the Hosting Bundle is installed before IIS, the bundle installation must be repaired. Run the Hosting Bundle installer again after installing IIS.
Create IIS Site
Open IIS Manager & expand server node in connections panel on left hand side. Right click site folder and select “Add Website…” from the context menu. Provide Site name, Physical path (where code was published), binding configuration details & click on ok.
Finally browse and check your website.
ASP.NET Core hosting on IIS supports two models for hosting on the IIS i.e. In-process hosting model & out-of-process hosting model. .NET Core 2.2 onward there were significant changes in the hosting model to IIS.
In-process hosting model provides a significant performance improvement over the other out-of-process model as it does not have to forward requests to Kestrel for processing
Internet Information Server (IIS) is a web server from Microsoft that gets shipped with Windows Operating System. All the requests made to Web Serves are recorded in IIS Logs.
Configure IIS Logs
You can configure IIS logs to capture details of all the HTTP requests along with status that is processed by the sites hosted on that IIS. IIS logs can help you troubleshoot your site.
IIS logs can be configured at the Server or Site level. I personally prefer configuring logging at the Server level as this sets a default logging configuration for all the new sites.
IIS Manager can be used to configure logging. Note that HTTP Logging feature should be installed (refer image below) for logging to be available
Configure or change setting using IIS Manager as shown below
Based on your requirements you can configure the following fields:
One log file per Site or Server, the default value is site & I personally prefer site.
Log File Format – IIS, NCSA, W3C, or Custom. W3C is a customizable option with different fields so you can log important properties and limiting the log size if required.
Select Fields to be Logged (Only available for file format W3C). The detailed list of Fields is covered in the next section.
Directory where logs will be saved
Log Event Destination
Logfile rollover settings
List of IIS W3C Log Fields
Here is the complete list of fields that are available for logging in the IIS logging module under log file format W3C.
Date (date): date of request.
Time (time): time of the request in Coordinated Universal Time (UTC).
Client IP Address (c-ip): the client IP address that made the request.
User Name (cs-username): the name of the authenticated user who made the request. A hyphen indicates an anonymous user.
Service Name (s-sitename): the site service name and instance number that handled the request.
Server Name (s-computername): the name of the server on which request was made.
Server IP Address (s-ip): the IP address of the server on which request was made.
Server Port (s-port): the server port number that is configured for the service.
Method (cs-method): the requested action, for example, a GET method.
URI Stem (cs-uri-stem): the URI, or target, of the action.
URI Query (cs-uri-query): the query, if any, that the client was trying to perform. A URI query is necessary only for dynamic pages.
Protocol Status (sc-status): the HTTP request status code.
Protocol Substatus (sc-substatus): the HTTP request substatus error code.
Win32 Status (sc-win32-status): the Windows status code.
Bytes Sent (sc-bytes): the number of bytes that the server sent to the client.
Bytes Received (cs-bytes): the number of bytes that the server received from the client.
Time Taken (time-taken): the time that the request took to complete (in milliseconds).
Protocol Version (cs-version): the HTTP protocol version that the client used.
Host (cs-host): the hostname, if any.
User Agent (cs(UserAgent)): the browser type that client used for request.
Cookie (cs(Cookie)): the content of the cookie sent or received.
Referrer (cs(Referrer)): the site that the user last visited. This site provided a link to the current site.
IIS Logs Location
Normally default location for IIS Log files is %SystemDrive%\inetpub\logs\LogFiles. But one can even change this location and log to a different directory.
To verify or change IIS logs file directory follow below steps
1. Start IIS Manager (Version 8.5) by either from Start=>run “intetmgr” or from Control Panel=>Administrative Tools=>Internet Information Services (IIS) Manager
2. In IIS Manager from Connections Tree on the left Panel select the site for which Logs directory needs to be configured. From Feature View in center double click on Icon Logging
On the Logging screen under Log File=>Directory you can find the configured location for IIS Log Files. Alternatively, you can click on action “View Log Files” to navigate directly to the logs folder in Windows Explorer.
Analyze IIS Logs
You can use one of the various available tools to analyze IIS logs. One of the tools is WebLog Expert which provides a complete analysis report from IIS Logs.
You can also manually analyze IIS Logs using the free tool provided by Microsoft i.e. Log Parser. Log Parser is a command-line tool used to query IIS Logs. In fact, as per Microsoft, Log Parser is universal query access to text-based data such as log files, XML, CSV & Windows Event Logs. Log Parser supports a variety of output formats.
Log parser can be downloaded from this link. Installation is pretty easy. Alternatively, Log Parser Studio that is built on top of the log parser can also be used. Log Parser Studio is a graphical user interface based tool that comes with many predefined queries that can be used for analysis.