Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.Net core 2.0 console app as a windows service

I am trying to set up a .Net Core Console application as a service. Using .Net standard I usually make use of Topshelf but this does not seem to support .Net Core.

Since Topshelf is not an option what can I do to run the the .Net Core Console application as a Windows service?

I have come access https://github.com/PeterKottas/DotNetCore.WindowsService, is this a viable substitute for a production environment?

like image 891
Matthew P. Avatar asked May 22 '18 22:05

Matthew P.


People also ask

Can I run console app as Windows Service?

NET Windows apps that can run as console apps or as Windows Services. You can quickly hook up events such as your service Start and Stop events, configure using code e.g. to set the account it runs as, configure dependencies on other services, and configure how it recovers from errors.

Does .NET Core support Windows Service?

NET Core and . NET 5+, developers who relied on . NET Framework could create Windows Services to perform background tasks or execute long-running processes. This functionality is still available and you can create Worker Services that run as a Windows Service.


Video Answer


2 Answers

There are a few options:

  1. Use the Windows Compatibility Pack which brings back ServiceBase and related APIs to .NET Core, so TopShelf may work out of the box. (the pack is in RC at the time of writing with scheduled stable release about a week away)

  2. TopShelf now supports .NET Core, based on the compatibility pack / windows compatibility for newer .NET Core versions.

  3. https://github.com/PeterKottas/DotNetCore.WindowsService seems to work for a lot of users so I suggest giving it a try.

  4. The library in (3) is a nice API around DasMulli.Win32.ServiceUtils which I wrote for our company to be able to deploy self-contained .NET Core Applications in production. So far we haven't had any problems.

  5. Other service hosts which abstract the windows service infrastructure and run arbitrary programs - such as the Non-Sucking Service Manager. Note that this may not give good ways to gracefully shut down in reaction to a Stop command.

like image 112
Martin Ullrich Avatar answered Oct 21 '22 06:10

Martin Ullrich


In .net core you do not have the option to run as Service. However, if you containerize your console application you can deploy the container anywhere and it is just the same as running as a service. From .NET Core 2.1 on you are able to use the Host and HostBuilder to manage DI, Logging, Graceful shut down, etc in you console app. Have a look at:

Hosting services in .NET Core console application

like image 2
Peter Avatar answered Oct 21 '22 05:10

Peter