Creating a Windows Service using .Net Core 2.2

Up until very recently, creating a Windows Service was the domain of the .Net Framework. However, since the release of the Windows Compatibility Pack that has all changed. In this article, we’ll create a .Net Core Windows Service from scratch.

I’m using the preview version of Visual Studio 2019 for this post. As far as I’m aware, there is absolutely no functional difference between this and VS2017; however, the initial New Project screen does look a little different.

Create the Project

There are no “New Windows Service (.Net Core)” options in Visual Studio, so we’re just going to create a console application (everything is a console application in .Net Core):

The .Net Core application can target .Net Core 2.2:

Windows Compatibility

The next step is to install the Windows Compatibility NuGet package:

Install-Package Microsoft.Windows.Compatibility

Write the Service

Let’s start with the main method:

static void Main(string[] args)
    using (var service = new TestSevice())

You’ll need to Ctrl-. ServiceBase. TestService doesn’t exist yet, so let’s create that:

internal class TestSevice : ServiceBase
    public TestSevice()
        ServiceName = "TestService";

    protected override void OnStart(string[] args)
        string filename = CheckFileExists();
        File.AppendAllText(filename, $"{DateTime.Now} started.{Environment.NewLine}");

    protected override void OnStop()
        string filename = CheckFileExists();
        File.AppendAllText(filename, $"{DateTime.Now} stopped.{Environment.NewLine}");

    private static string CheckFileExists()
        string filename = @"c:\tmp\MyService.txt";
        if (!File.Exists(filename))

        return filename;


Not exactly a complicated service, I’ll grant you.


For Framework apps, you could use InstallUtil, but if you try that on a Core app, you get an annoyingly vague error! Instead, you need to find the place where the binary has been compiled; for example:


Now, launch a command prompt as admin, and type the following:

>sc create [service name] binpath=[full path to binary]

For example:

>sc create pcmtestservice binpath=C:\Users\pcmic\source\repos\ConsoleApp3\ConsoleApp3\bin\Debug\netcoreapp2.2\ConsoleApp3.exe

You should get the response:

[SC] CreateService SUCCESS

You can then either start the service from here:

>sc start pcmtestservice

Or locate it in the services utility and start it from there. You should now be able to start and stop the service and see it logging the events as you do so.

If you need to remove the service, use:

>sc delete pcmtestservice


12 thoughts on “Creating a Windows Service using .Net Core 2.2

  1. Renin

    Thank you so much for the information which is very clear. One thing I faced while implementing this is , I couldn’t generate a exe file, instead I am getting the dll. I have gone through several articles , but may I know if you have any suggestions ?

  2. pcmichaels Post author

    I had a similar issue myself. I was initially under the impression that you could simply create a class library and the service controller would manage it: but you need to create it as a console application. If you have done, then check the properties of the console app project to check the output type is set to exe.

    1. Renin

      Thank you for the response. To be honest I tried to put the output type as Windows Application already (Remaining two are console and class library). But still it is generating the dlls 🙁

      1. pcmichaels Post author

        For this particular article, you’ll need console app – which is what it will be set to if you generate a console app from scratch. If you change it to console app and you don’t start getting an exe, make sure you’re looking in the right directory: remember that the binaries will only be in the debug directory above if you’re running in debug mode; otherwise they’ll be in release.

  3. Dan

    Very helpful!
    I came across several articles with instructions on creating a windows service in .net core, some even for 2.2, but none as straightforward and simple as this. I assume that with 2.2 the process has become much simpler, and the other articles just weren’t up-to-date.
    About the dll/exe problem that the other readers have described – I had the same problem.
    After reading this:
    I realized that it’s actually by design, and only by adding a RuntimeIdentifier to the csproj, an exe will generated:


    Actually the app itself is still generated as a dll, but now an additional exe with the same same is generated, which only serves to run the dll.
    From your comment it appears that you get an exe without this fiddling. Can you please explain how?

    1. Dan

      Sorry, I didn’t realize I should have escaped the brackets. The csproj snippet should be:

  4. SteveHarveyUK

    Hi Dan,
    Found this article and am trying it on a project that was previously .NET Framework, with a runtime Application/Service using the ServiceBase and ServiceBase.Run() pattern. I’m seeing something really bizarre.
    Everything builds and both net472 & netcoreapp2.2 work as console apps. The net472 service version also works fine, but the netcoreapp2.2 service just gets a StartService FAILED 1053 message even though I can see logging indicating that it’s working. Obviously, the netcoreapp2.2 service then get shut down.

    It’s very frustrating to be so close… Both frameworks are using the exact same ServerBase implementation. Any ideas what it could be?

    1. pcmichaels Post author

      Have you checked the windows app logs? It might give you some better information about why it failed.

      1. SteveHarveyUK

        Looks like I was missing something silly. Apparently the switch I’m using to toggle between console and service mode doesn’t work when running as a netcoreapp2.2 service. Basically, Environment.UserInteractive = True in netcreapp2.2 while it is false in net472.

  5. Dheeraj Kumar


    thanks to your blog i am able to create a window service on .NET Core 2.2 and install it on Windows 10 Machine/laptop.

    I am not able to install it on Windows 2016 Std. 64 bit properly. When i run SC create command then it run successfully and service gets created successfully.

    but when i try to run it gives me (within 1 sec) Error 1053

    can you Please help me here to resolve this issue.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.