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 “Sample .Net Core Service Hosting” for more details on how it was implemented and GitHub repository for sample code. The completed project is branched to service_host_daemon branch.

There are several ways we could go to extend it to run as Windows Service, console app and Linux daemon. One way is to have a separate .Net Core project that will host our sample generic host service for each case or allow the program to accept command line variables that specify how to run the program. We will implement the command line arguments.

Command-line options:

  • -i    Install as Windows Service
  • -u  Uninstall Windows Service
  • -console Run as a console app
  • -daemon Run as Linux daemon service
  • -h  Show command line switch help

Let’s make Argument Parser

In order to parse the arguments passed to the application, we will implement an argument parser class, which will return HostAction enum that specifies how the application is going to start. The application will accept only one argument on its input. If more than one argument supplied or it is an invalid argument, it will show usage message. If no arguments supplied it will try to run as Windows Service.

HostAction enum

namespace VSC
{
    public enum HostAction
    {
        ShowUsage,
        InstallWinService,
        UninstallWinService,
        RunWinService,
        WinServiceStop,
        RunConsole,
        RunLinuxDaemon
    }
}

Argument Parser class – ArgsParser

namespace VSC
{
    internal class ArgsParser
    {
        private string[] _args = null;
        public ArgsParser(string[] args)
        {
            _args = args;
        }

        public HostAction GetHostAction()
        {
            HostAction action = HostAction.ShowUsage;

            if(_args == null || _args.Length == 0)
            {
                action = HostAction.RunWinService;
            }
            else if(_args.Length > 1)
            {
                action = HostAction.ShowUsage;
            }
            else
            {
                string argument = _args[0];

                if(argument == "-i")    // install
                {
                    action = HostAction.InstallWinService;
                }
                else if(argument == "-u")   // uninstall
                {
                    action = HostAction.UninstallWinService;
                }
                else if(argument == "-console")
                {
                    action = HostAction.RunConsole;
                }
                else if (argument == "-daemon")
                {
                    action = HostAction.RunLinuxDaemon;
                }
            }

            return action;
        }
    }
}

Let’s make HostedService class

At the moment sample_service_hosting app creates host builder by calling CreateHostBuilder() from the Main function. We need to introduce a class that will perform an action depending on what HostAction returned by ArgsParser. We will call it HostedService class. It will have an async Run function that will start different action.

For now let’s implement run as a console, run as Linux daemon and show usage actions. Function CreateHostBuilder() will be moved from Program.cs to HostedService class.

HostedService class

using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using NLog.Extensions.Hosting;

namespace VSC
{
    internal class HostedService
    {
        private static NLog.Logger _logger = NLog.LogManager.GetCurrentClassLogger();

        private ArgsParser _argsParser = null;

        public HostedService(ArgsParser argsParser)
        {
            _argsParser = argsParser;
        }

        public async Task Run(string[] args)
        {
            if (_argsParser != null)
            {
                var builder = this.CreateHostBuilder(args);
                
                if(builder == null) return;

                switch (_argsParser.GetHostAction())
                {
                    case HostAction.InstallWinService:
                    {
                        
                    }
                    break;
                    case HostAction.UninstallWinService:
                    {
                        
                    }
                    break;
                    case HostAction.RunWinService:
                    {
                        
                    }
                    break;
                    case HostAction.RunConsole:
                    {
                        try
                        {
                            await builder.RunConsoleAsync();
                        }
                        catch(Exception ex)
                        {
                            _logger.Error("Could not run as console app. " + ex.Message);
                            ShowUsage();
                        }
                    }
                    break;
                    case HostAction.RunLinuxDaemon:
                    {
                        try
                        {
                            await builder.Build().RunAsync();
                        }
                        catch(Exception ex)
                        {
                            _logger.Error("Could not start as Linux daemon service. " + ex.Message);
                            ShowUsage();
                        }
                    }
                    break;
                    default:
                    {
                        ShowUsage();
                    }
                    break;
                } 
            }   
        }

        private void ShowUsage()
        {   
            Console.WriteLine("Options:\n"
                        + "  'no options'\tStart Windows Service\n"
                        + "  -i\t\tInstall as Windows Service\n"
                        + "  -u\t\tUninstall Windows Service\n"
                        + "  -console\tRun as console app\n"
                        + "  -daemon\t Run as Linux daemon service\n"
                        + "  -h\t\tShow command line switch help\n");
        }
        private IHostBuilder CreateHostBuilder(string[] args)
        {
            try
            {
                var builder = new HostBuilder()
                    .UseNLog()
                    .ConfigureAppConfiguration((hostingContext, config) =>
                    {
                        config.SetBasePath(Directory.GetCurrentDirectory());
                        config.AddJsonFile("appsettings.json", optional: true);
                        config.AddJsonFile(
                            $"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", 
                            optional: true);
                        config.AddCommandLine(args);
                    })
                    .ConfigureServices((hostContext, services) =>
                    {
                        services.AddHostedService<Services.SampleService>();
                        services.Configure<HostOptions>(option =>
                        {
                            option.ShutdownTimeout = System.TimeSpan.FromSeconds(20);
                        });
                    })
                    .ConfigureLogging((hostingContext, logging) => 
                    {
                        logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
                        logging.AddConsole();
                    });

                return builder;
            }
            catch { }

            return null;
        }
    }
}

In Program.cs we change code as follows.

Program.cs

using System.Threading.Tasks;

namespace VSC
{
    class Program
    {   
        static async Task Main(string[] args)
        {
            ArgsParser argsParser = new ArgsParser(args);

            HostedService service = new HostedService(argsParser);
            await service.Run(args);
        }
    }
}

If we edit in VS Code launch.json and set “args”: [“-console”] or “args”: [“-daemon”] app will run our SampleService and log messages.

Let’s Make it self installable Windows Service

Now we got to the point of making our sample app to run as Windows Service. As Software Developer we always try to make the life of our users easier. If the app will be able to self install/uninstall as windows service our users will be much happier than trying to do that themselves.

We will start in the external process sc.exe service controller tool with parameters to install/uninstall/stop service. Example:

  • sc.exe create “service_name” displayname= “service_name” binpath= “path to exe file”
  • sc.exe delete “service_name”
  • sc.exe stop “service_name”

WinServiceInstaller class will be responsible for running an external process and notifying of the progress. We will use WinService class to handle Start, Stop events and use extension functions to inject our WinService class into our Generic Host Builder.

WinService class

using System;
using System.ServiceProcess;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;

namespace VSC
{
    public class WinService : ServiceBase, IHostLifetime
    {
        public static string WinServiceName = "Default Service Name";
        private readonly TaskCompletionSource<object> _delayStart = new TaskCompletionSource<object>();

        private IApplicationLifetime ApplicationLifetime { get; }

        public WinService(IApplicationLifetime applicationLifetime)
        {
            this.ServiceName = WinServiceName;

            ApplicationLifetime = applicationLifetime ?? throw new ArgumentNullException(nameof(applicationLifetime));
        }        

        public void Start()
        {
            try
            {
                Run(this); // This blocks until the service is stopped.
                _delayStart.TrySetException(new InvalidOperationException("Stopped without starting"));
            }
            catch (Exception ex)
            {
                _delayStart.TrySetException(ex);
            }

            this.OnStart(null);
        }

        public Task StopAsync(CancellationToken cancellationToken)
        {
            Stop();
            return Task.CompletedTask;
        }

        public Task WaitForStartAsync(CancellationToken cancellationToken)
        {
            cancellationToken.Register(() => _delayStart.TrySetCanceled());
            ApplicationLifetime.ApplicationStopping.Register(Stop);

            new Thread(Start).Start(); // Otherwise this would block and prevent IHost.StartAsync from finishing.
            return _delayStart.Task;
        }

        protected override void OnStart(string[] args)
        {
            _delayStart.TrySetResult(null);
            base.OnStart(args);
        }

        protected override void OnStop()
        {
            ApplicationLifetime.StopApplication();
            base.OnStop();
        }
    }
}

WinServiceInstaller class

using System.Diagnostics;

namespace VSC
{
    public static class WinServiceInstaller
    {
        private static NLog.Logger _logger = NLog.LogManager.GetCurrentClassLogger();

        public static string APP_EXECUTABLE_PATH = string.Empty;
        private const string ServiceControllerEXE = "sc.exe";

        public delegate void WinServiceStatusHandler (string status);
        public static event WinServiceStatusHandler WinServiceStatus;

        public static void Uninstall(string serviceName)
        {
            Stop(serviceName); // stop service before uninstall

            RaiseWinServiceStatus("Uninstall Service");

            RunProcess(string.Format("delete \"{0}\"", serviceName));
        }

        private static void Stop(string serviceName)
        {
            RaiseWinServiceStatus("Stopping Service");

            RunProcess(string.Format("stop \"{0}\"", serviceName));            
        }

        public static void Install(string serviceName)
        {
            if(!string.IsNullOrEmpty(APP_EXECUTABLE_PATH))
            {
                RaiseWinServiceStatus("Install Service");

                string processArguments = string.Format("create \"{0}\" displayname= \"{1}\" binpath= \"{2}\"", serviceName, serviceName, APP_EXECUTABLE_PATH);
                
                RunProcess(processArguments);
            }
            else
            {
                _logger.Error("Cannot install service. Path to exe cannot be empty.");
            }
        }

        private static void RaiseWinServiceStatus(string status)
        {
            if(WinServiceStatus != null)
            {
                WinServiceStatus(status);
            }
        }

        private static void RunProcess(string arguments)
        {
            _logger.Trace("Arguments: " + arguments);

            var process = new Process();
            var processInfo = new ProcessStartInfo();
            processInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            processInfo.FileName = ServiceControllerEXE;
            processInfo.Arguments = arguments;
            process.StartInfo = processInfo;
            process.Start();
            process.WaitForExit();
        }
    }
}

WinServiceExtensions static class

using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace VSC
{
    public static class WinServiceExtensions
    {
        internal static IHostBuilder UseServiceBaseLifetime(this IHostBuilder hostBuilder)
        {
            return hostBuilder.ConfigureServices((hostContext, services) => services.AddSingleton<IHostLifetime, WinService>());
        }

        public static Task RunAsWindowsServiceAsync(this IHostBuilder hostBuilder, CancellationToken cancellationToken = default)
        {
            return hostBuilder.UseServiceBaseLifetime().Build().RunAsync(cancellationToken);
        }
    }
}

We need to add to our project package reference System.ServiceProcess.ServiceController in order to compile WinService class.

dotnet add package System.ServiceProcess.ServiceController

Now in HostedService Run function, we can implement remaining switch cases that will Install, Uninstall and Run our app as windows service.

internal class HostedService
{
    ...

    public async Task Run(string[] args)
    {
        if (_argsParser != null)
        {
            var builder = this.CreateHostBuilder(args);
                
            if(builder == null) return;

            WinServiceInstaller.APP_EXECUTABLE_PATH = Utility.GetExecutingAssemblyLocation().Remove(Utility.GetExecutingAssemblyLocation().Length - 4) + ".exe";
                
            switch (_argsParser.GetHostAction())
            {
                case HostAction.InstallWinService:
                {
                    WinServiceInstaller.Install(WinService.WinServiceName);
                }
                break;
                case HostAction.UninstallWinService:
                {
                    WinServiceInstaller.Uninstall(WinService.WinServiceName);
                }
                break;
                case HostAction.RunWinService:
                {
                    try
                    {
                        await builder.RunAsWindowsServiceAsync();
                    }
                    catch(Exception ex)
                    {
                        _logger.Error("Could not start as windows service. " + ex.Message);
                        ShowUsage();
                    }
                }
                break;

    ...
}

You may have noticed that APP_EXECUTABLE_PATH is set by calling utility function GetExecutingAssemblyLocation in Utility class.

Utility static class

namespace VSC
{
    public static class Utility
    {
        public static string GetExecutingAssemblyLocation()
        {
            return System.Reflection.Assembly.GetExecutingAssembly().Location;
        }
    }
}

Let’s update the Main function

As the final step, we will update our Program.cs and add the logger to print status of our Windows Service. At the end before closing application, we also need to Flush logger and shut it down to release resource when our app will run as Linux daemon.

Program.cs

using System;
using System.Threading.Tasks;
using NLog;
using System.Reflection;

namespace VSC
{
    class Program
    {   
        private static Logger _logger = NLog.LogManager.GetCurrentClassLogger();

        static async Task Main(string[] args)
        {
            ArgsParser argsParser = new ArgsParser(args);

            WinService.WinServiceName = "The Sample Service Host";
            WinServiceInstaller.WinServiceStatus += new WinServiceInstaller.WinServiceStatusHandler(PrintWinServiceStatus);

            _logger.Info("Version: " + Assembly.GetEntryAssembly().GetName().Version.ToString());

            HostedService service = new HostedService(argsParser);
            await service.Run(args);

            _logger.Info("Shutting down logger...");
            // Flush buffered log entries before program exit; then shutdown the logger before program exit.
            LogManager.Flush(TimeSpan.FromSeconds(15));
            LogManager.Shutdown();
        }

        private static void PrintWinServiceStatus(string status)
        {
            _logger.Info(status);
        }
    }
}

Summary

Today we extended sample_service_hosting app with the ability to run as windows service and self install/uninstall itself to make it easier to deploy as windows service. We could go down the path of implementing separate projects: one for a console app, one for Linux daemon, one for windows service and one common project with our SampleService Host. We managed to do it all in one project with the use of ArgsParser class and extension functions to inject our Host into WinService.

SHARE ON

The Author: Vlad Setchin

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

Leave a Reply

Your email address will not be published.