PM> Install-Package Shuttle.Core.ServiceHost

Allows you to host your console application. When using .Net 4.6+ your console may also be hosted as a Windows Service. When using .Net Core 2.0+ your console hosting can be stopped by sending ctrl+c to the console.

A typical implementation would be as follows:

using System;
using System.Threading;
using System.Threading.Tasks;
using Shuttle.Core.Threading;

namespace Shuttle.Core.ServiceHost.Server
    internal class Program
        private static void Main()

        public class Host : IServiceHost
            private Task _task;
            private CancellationTokenSource _cancellationTokenSource;
            private CancellationToken _cancellationToken;

            public void Start()
                _cancellationTokenSource = new CancellationTokenSource();
                _cancellationToken = _cancellationTokenSource.Token;

                _task = Task.Run(() =>
                    while (!_cancellationToken.IsCancellationRequested)
                        Console.WriteLine($"[working] : {DateTime.Now:O}");
                        ThreadSleep.While(1000, _cancellationToken);
                }, _cancellationToken);

            public void Stop()

Implement the IServiceHost interface if you need both Start() and Stop() methods; else IServiceHostStart for Start() and IServiceHostStop for Stop() although there would be little value in having only a Stop(). If you do not need a Stop() method or you prefer using IDisposable to handle the destruction then you would go with only the IServiceHostStart interface.

Running the host

The following methods are available to get this going on the ServiceHost class:

public static void Run<T>() where T : IServiceHostStart, new()
public static void Run(IServiceHostStart service)

For .Net 4.6+ the following are also available:

public static void Run<T>(Action<IServiceConfiguration> configure) where T : IServiceHostStart, new()
public static void Run(IServiceHostStart service, Action<IServiceConfiguration> configure)

The IServiceConfiguration allows you to configure the service from code. Configuration supplied through code has the highest precedence.

.Net 4.6+ options

Configuration Section

You may also specify configuration using the following configuration which may, as all Shuttle configuration sections do, be grouped under a shuttle group.

    <section name="service" type="Shuttle.Core.ServiceHost.ServiceHostSection, Shuttle.Core.ServiceHost" />

    startMode="Disabled" />

Command Line (.Net 4.6+ only)

The following command-line arguments are available and can be viewed by running {your-console}.exe /?:

[/install [/serviceName]]    
    - install the service
    - friendly name for the installed service
    - Description for the service
    - unique name of the instance you wish to install
    - specifies that the service start mode (Boot, System, Automatic, Manual, Disabled)
    - if specified will delay services with a start mode of 'Automatic'

[/username /password]
    - username and password of the account to use for the service

- or -
[/uninstall [/serviceName] [/instance]]    

    - starts the service instance

    - stops the service instance

Service Name

If no /serviceName is specified the full name of the your console application along with the version number, e.g.:

Namespace.ConsoleApplication (


If you set the /serviceName and/or /instance during installation you will need to specify them when uninstalling as well, e.g.:



    /displayName:"Shuttle server for the application"
    /description:"Service to handle messages relating to the application."