Running .Net Core Generic Host as a Windows Service, Linux Daemon or Console App

Net Core 2.2 provides Web Host for hosting web applications and Generic Host that is suitable for hosting a wider array of host apps. In the .Net Core 3 Generic Host will replace Web Host.  Generic Host will be suitable for hosting any kind of app including web applications.

I looked at many examples but found that none showed the complete implementation of the Generic Host that is implemented to run as a console, Linux daemon, and Windows Service all in one application.

Here we will be looking at extending sample_service_hosting app to run it as a console app, windows service, or Linux daemon as well as making it self installable windows service.

.Net Core provides IHostBuilder interface to configure and create host builder. In case of a console app, host builder will run await RunConsoleAsync() function. To host and run our Generic Host as Windows Service we will need to use IApplicationLifetime to register Start and Stop events.

For hosting our Generic Host in Linux daemon we are going to inject IApplicationLifetime into main service and register and handle Start and Stop events.

Our sample project sample_service_hosting console application is already implemented to run as Linux daemon. See “