Install .Net 5.0 worker service application as windows service withing program using start parameter

Thomas Gössi 1 Reputation point


I programmed a windows service using the worker service under .Net Core 5.0 . In the template description I found, that the service can be installed and uninstalled using the exe with console parameters install or uninstall respectively.
I also found, that this works find in .Net Core 3.1 (did not test that).

Eg. if my program is called hostservice.exe, the service could be installed starting hostservice.exe -install

But this is not working.

However, each program can be installed as a service using sc.exe, but I don't want to use sc.exe. Using the program with parameter install is more comfortable.

Is that function not implemented anymore in .Net Core 5.0 or how can achieve this to use install/uninstall to installing/uninstalling the program as a windows service. Maybe I forgot to call any method on the worker service to do this.

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,606 questions
.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,137 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Michael Taylor 50,996 Reputation points

    Any command line handling of install/uninstall would require that the code be written to do that. It won't do it automatically. I'm not sure where you see a description that says it will do this automatically. On my currently updated version of VS 2019 the only worker service option for .NET 5 is an empty template.

    If you look at pretty much every example they will all use sc. This makes sense in most cases because you have to have admin privileges to install a service and is generally installed using an installer program. However I suspect you might be looking at an example of using the third party library Topshelf. This third party library makes it easier to build Windows services and it does support the ability to specify an install/uninstall option from the command line. Under the hood it is going to require that you write code to determine whether to install/uninstall from command line (or whatever) but otherwise it handles the details. Refer to its documentation on how to do this.

    0 comments No comments