Skip to content

Sample repository to run .NET Core and ASP.NET Core as a Windows Service

Notifications You must be signed in to change notification settings

holger-romey/AspNetCoreWindowsService

 
 

Repository files navigation

AspNetCoreWindowsService

This is a simple sample repository to show how simple it is to run .NET Core and ASP.NET Core as a Windows Service.

I tried to keep it bare minimum in order to appreciate what is needed. You can probably modify your current .NET Core API or ASP.NET MVC Project just by copy-pasting a few lines of this code to make it run as a Windows Service!

Highlights:

  1. You have to target .NET 4.6.1 framework. Since this application is intented to run as a Windows Service it makes sense since Linux or Mac don't have a Windows service so it can't be part of the .NET Core framework.

    [project.json]

    "frameworks": { "net461": { } },

  2. You have to use Kestrel web server when running as a Windows Service.

    [Program.cs]

    var host = new WebHostBuilder() .UseKestrel() .UseContentRoot(contentPath) .UseStartup() .Build();

  3. If you are using MVC you need to set the ContentRoot directory.

    [Program.cs]

    if (isConsole) contentPath = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName); else { contentPath = Directory.GetCurrentDirectory(); }

  4. By default it runs as a service unless it detects you are Debugging or you pass the command line parameter --console

    [Program.cs]

    if (Debugger.IsAttached || args.Contains("--console")) { isConsole = true; } else { isConsole = false; }

About

Sample repository to run .NET Core and ASP.NET Core as a Windows Service

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 100.0%