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 Timer
causes 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
Topshelf
NuGet package for your .NET version. Note: TheExamplePeriodicService
project is using .NET 4.5 and the Topshelf version used is 3.3.154.0. - Add the most recent stable version of
log4net
NuGet package for your .NET version. TheExamplePeriodicService
project is using .NET 4.5 andlog4net
version 2.0.5. - Add a new class and inherit from
PeriodicServiceBase
class. - Implement the following methods required when inheriting from
PeriodicServiceBase
- constructor with
int
paramater 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
Program
class. 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.config
file and copy the code from the ExamplePeriodicService project. Be sure to setCopy always
on the file properties and update the<file>
value attribute in accordance with the comment in thelog4net.config
file. This configure sets up a rolling file appender and a console appender for log4net.