Publish an ASP.Net Core 1 Application to IIS

In this tutorial, we are going to see how to publish an ASP.Net Core 1 Application to IIS. Unlike older versions of ASP.Net, ASP.Net Core 1 applications are not dependent on IIS for hosting. In fact ASP.Net Core 1 applications are console applications and run in their own Kestrel Web Server. But if we need to have more functionalities which IIS provides by default (or through extensions), then we can use IIS as reverse proxy which will forward requests to Kestrel.

When we run IIS as a reverse proxy for Kestrel, IIS uses a native module AspNetCoreModule (which sits in IIS pipeline), which will execute (through dotnet run command of Dotnet CLI) the external ASP.Net Core 1 console application. Dotnet run command will load the Kestrel server and serves the initial request. For subsequent requests, AspNetCoreModule redirects to ASP.Net Core 1 Application. Once Kestrel gets the request, it will put it through the ASP.Net Core middleware and then routes it accordingly. Finally the output response is sent back to IIS which will push it back to the client (for example browser). AspNetCoreModule also ensures that application is always accessible even after a restart or crash.

Lets get started by installing AspNetCoreModule (which will be installed along with .Net Core SDK installer). If the module is not installed by default or if we just need a standalone installation, then we can download it from .Net Core Downloads under Windows (Server Hosting). Once installed, it will be listed in IIS Modules.

image

NOTE: Following tutorial has to be completed before proceeding with present tutorial Getting Started with ASP.Net Core 1 Application.

ASP.Net Core 1 Application is configured for IIS Integration through UseIISIntegration() extension method of WebHostBuilder() at Program.cs.We should still use UseKestrel() to host the application in Kestrel Web Server.

public static void Main(string[] args)
{
    var host = new WebHostBuilder()
        .UseKestrel()
        .UseContentRoot(Directory.GetCurrentDirectory())
        .UseIISIntegration()
        .UseStartup<Startup>()
        .Build();

    host.Run();
}

AspNetCoreModule is going to be configured via web.config of ASP.Net Core 1 Application. In web.config, we are going to mention the DLL which needs to be run under dotnet command. The other parameters include enabling the log output and log file. There is no need of manual work in generating this web.config, dotnet publish will take care of it.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <handlers>
      <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
    </handlers>
    <aspNetCore 
            processPath="dotnet" 
            arguments=".\AspNetCoreMvc.dll" 
            stdoutLogEnabled="false" 
            stdoutLogFile=".\logs\stdout" 
            forwardWindowsAuthToken="false" />
  </system.webServer>
</configuration>

First publish the application using dotnet publish command (open command line prompt in administrator mode and navigate to application’s directory). When we do initial publish, we might get following error. This error is due to the command bower install which will run as part of project.json’s prepublish to install the client side package dependencies through bower, and bower is not installed.

image

To resolve above error, we need to install bower as shown below.

image

Now publish the application.

image

All the published content is by default placed under \bin\Debug\netcoreapp1.0\publish folder. We can also publish the application in release configuration by running the command with –configuration release. There are many other options which we can set for dotnet publish command like output path, framework to be used etc. We will discuss the contents of publish folder in a different tutorial.

Now lets create an Application pool at IIS with No Managed code. We use no managed code because IIS is just going to act as proxy to forward requests, so there is no need of a .Net Run time at application pool.

image

Create a new Website in IIS with AspNetCoreAppPool.

image

Now when we browse localhost:8080, we will get the site up and running.

image

NOTE: We might have certain errors (like shown below). These are due to access permissions, I have given IIS_IUSRS user full control permissions on publish folder. Once given, we can have website up and running as usual.

image

image

That’s it for now. Happy Coding and Stay Tuned!!!

You may also like...