Run core web app as a windows service via IIS

Rick Strahl has a thorough article on publishing your dot net core app with IIS

This requires you to install the AspNetCoreModule into IIS and it is this module which manages the lifetime of your dot net core process which runs separately to w3wp.exe in its own process (e.g. WebApplication1.exe)

This is less than ideal if your web app runs background tasks that you want running constantly - it requires you to poll your website after a server or IIS reboot to ensure your process is loaded and running (via AspNetCoreModule)

It is possible to host the dotnet core app as a windows service but this wont work with AspNetCoreModule - the latter will simply ignore the running process and start its own new instance.

To get this working so you have your dotnet core app running as a service and have it hosted via IIS (importantly on ports 80,443 with other hosted web apps) the following steps are required: (assumes you are starting from a File -> New Project -> ASP.NET Core Web Application)

1) in project.json add references to

"Microsoft.AspNetCore.Hosting": "1.0.0-*",
"Microsoft.AspNetCore.Hosting.WindowsServices": "1.0.0-*",

This will result in an error

The dependency Microsoft.AspNetCore.Hosting.WindowsServices 1.0.0 does not support framework .NETCoreApp,Version=v1.0.

You need to target the full framework - this suited my purposes fine as i was running on windows only and depended on libraries not yet supported by dotnet core. If you absoutely must have this working on other platforms as a pure dotnet core app then check out dasMulli/dotnet-win32-service

Replace the entire frameworks section in project.json with

"frameworks": {
    "net461": {


and remove from dependencies this section

"Microsoft.NETCore.App": {
      "version": "1.0.1",
      "type": "platform"

2) Modify your Program.cs file:

if (Debugger.IsAttached || args.Contains("--debug"))
    host.RunAsService();//extension method in Microsoft.AspNetCore.Hosting.WindowsServices

3) Publish your web app to the file system and navigate to the output folder (e.g. WebApplication1\src\WebApplication1\bin\Release\PublishOutput)

4) Install and run app as a service (from admin command prompt)

sc create WebApplication1 binpath="full path to your exe"

You should now be able to navigate and test your web app via http://localhost:5000/api/values

5) Lastly you need to setup a proxy in IIS to route requests to your windows service: This step is largely copied from this article by Scott Forsyth

Create an application pool for the website that is No Managed Code (runtime not required for a simply proxy)

Do not point at your dotnet core output folder Instead just point it at an empty folder that IIS has access to. This folder will simply contain a web.config file with the rewrite rule. (don't use a temp folder in production either :))

alt text

Install Url Rewrite into IIS and add a reverse proxy

alt text

n.b. do not add http to the start of the address alt text

Start your website and navigate to http://localhost/api/values - hopefully it all works!

Posted by: Wallace Turner
Last revised: 08 Mar, 2017 07:07 AM History


No comments yet. Be the first!

No new comments are allowed on this post.