Creating a Periodic Windows Service - LessonsLearnedInDotNET/LL.NET.Topshelf GitHub Wiki
A periodic Windows service, in terms of this repository, is a service with a Timer that elapses every x minutes. Each elapse event triggered by the Timercauses some business logic to execute.
The TopShelf.StarterPack.Core contains an abstract class for accelerating development so that you get quickly get started with a periodic Topshelf Windows service and focus on business logic. The PeriodicServiceBase class is preconfigured with the methods necessary for a Topshelf Windows service that will raises periodic events based on the timer period passed to the constructor.
- Add a new Console Application to your solution.
- Add the most recent stable version of
TopshelfNuGet package for your .NET version. Note: TheExamplePeriodicServiceproject is using .NET 4.5 and the Topshelf version used is 3.3.154.0. - Add the most recent stable version of
log4netNuGet package for your .NET version. TheExamplePeriodicServiceproject is using .NET 4.5 andlog4netversion 2.0.5. - Add a new class and inherit from
PeriodicServiceBaseclass. - Implement the following methods required when inheriting from
PeriodicServiceBase- constructor with
intparamater e.g.:public ExampleDerivedPeriodicService(int period) : base(period) public override void OnStart()public override void OnStop()public override void OnPeriodExpired()
- constructor with
- Add business logic required for each method.
- Configure Topshelf and log4net with new service class in
Programclass. See Configuring a Topshelf service in a Console application for more details. You can also see the actual code in the ExamplePerioicService project. - Add the
log4net.configfile and copy the code from the ExamplePeriodicService project. Be sure to setCopy alwayson the file properties and update the<file>value attribute in accordance with the comment in thelog4net.configfile. This configure sets up a rolling file appender and a console appender for log4net.