Popular blog tags

DotNetCore.WindowsService-dot net core CONSOLE application as windows services

Published

DotNetCore.WindowsService https://github.com/PeterKottas/DotNetCore.WindowsService Simple library that allows one to host dot net core application as windows services. Perfect solution to power micro-services architecture. Important note This library was created to enable one to host CONSOLE dot net core applications. If you want to host a WEBSITE as a service, you're better of following https://docs.microsoft.com/en-us/aspnet/core/hosting/windows-service Installation Using nuget: Install-Package PeterKottas.DotNetCore.WindowsService Quick start Easiest way to start is using a brand new template. Just do : dotnet new -i PeterKottas.DotNetCore.WindowsService.Templates::* This will add one template at the moment. Follow up with this mkdir NameOfYourProject cd NameOfYourProject dotnet new mcrsvc-min This will create a sample project for you. Next chapter explains its features in more details especially points 6 onwards if you used the template. Community, feel encouraged to add more templates if you find something missing/usefull. I'll be more than happy to add these. Just copy the project in https://github.com/PeterKottas/DotNetCore.WindowsService/tree/master/Source/Templates/PeterKottas.DotNetCore.WindowsService.MinimalTemplate and follow instructions in https://github.com/dotnet/templating if you need more specific behvaiour. Usage Create .NETCore console app. Create your first service, something like this: public class ExampleService : IMicroService { public void Start() { Console.WriteLine("I started"); } public void Stop() { Console.WriteLine("I stopped"); } } You can also inherit MicroService base class and take advantage of built in timers: public class ExampleService : MicroService, IMicroService { public void Start() { this.StartBase(); Timers.Start("Poller", 1000, () => { Console.WriteLine("Polling at {0}\n", DateTime.Now.ToString("o")); }, (e) => { Console.WriteLine("Exception while polling: {0}\n", e.ToString()); }); Console.WriteLine("I started"); } public void Stop() { this.StopBase(); Console.WriteLine("I stopped"); } } Api for services (and yeah, it's simmilar to Topshelf, thanks for inspiration, I just couldn't wait for you guys to implement this): ServiceRunner<ExampleService>.Run(config => { var name = config.GetDefaultName(); config.Service(serviceConfig => { serviceConfig.ServiceFactory((extraArguments) => { return new ExampleService(); }); serviceConfig.OnStart((service, extraArguments) => { Console.WriteLine("Service {0} started", name); service.Start(); }); serviceConfig.OnStop(service => { Console.WriteLine("Service {0} stopped", name); service.Stop(); }); serviceConfig.OnInstall(service => { Console.WriteLine("Service {0} installed", name); }); serviceConfig.OnUnInstall(service => { Console.WriteLine("Service {0} uninstalled", name); }); serviceConfig.OnPause(service => { Console.WriteLine("Service {0} paused", name); }); serviceConfig.OnContinue(service => { Console.WriteLine("Service {0} continued", name); }); serviceConfig.OnError(e => { Console.WriteLine("Service {0} errored with exception : {1}", name, e.Message); }); }); }); Optionally set the name of the service like this: ServiceRunner<ExampleService>.Run(config => { config.SetName("MyTestService"); }); Run the service without arguments and it runs like console app. Run the service with action:install and it will install the service. Run the service with action:uninstall and it will uninstall the service. Run the service with action:start and it will start the service. Run the service with action:stop and it will stop the service. Run the service with username:YOUR_USERNAME, password:YOUR_PASSWORD and action:install which installs it for the given account. Run the service with built-in-account:(NetworkService|LocalService|LocalSystem) and action:install which installs it for the given built in account. Defaults to LocalSystem. Run the service with description:YOUR_DESCRIPTION and it setup description for the service. Run the service with display-name:YOUR_DISPLAY_NAME and it setup Display name for the service. Run the service with name:YOUR_NAME and it setup name for the service. Run the service with start-immediately:(true|false) to start service immediately after install. Defaults to true. You can find the complete example in PeterKottas.DotNetCore.Example project. Install the service using powershell: New-Service -Name $serviceName -BinaryPathName "$binPath action:run" -StartupType Automatic .