Creating a Periodic Windows Service - LessonsLearnedInDotNET/LL.NET.Topshelf GitHub Wiki

What is a periodic Windows service?

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.

StarterPack Helper

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.

Creating a periodic Windows service using PeriodicServiceBase

  1. Add a new Console Application to your solution.
  2. Add the most recent stable version of Topshelf NuGet package for your .NET version. Note: The ExamplePeriodicService project is using .NET 4.5 and the Topshelf version used is 3.3.154.0.
  3. Add the most recent stable version of log4net NuGet package for your .NET version. The ExamplePeriodicService project is using .NET 4.5 and log4net version 2.0.5.
  4. Add a new class and inherit from PeriodicServiceBase class.
  5. Implement the following methods required when inheriting from PeriodicServiceBase
    1. constructor with int paramater e.g.: public ExampleDerivedPeriodicService(int period) : base(period)
    2. public override void OnStart()
    3. public override void OnStop()
    4. public override void OnPeriodExpired()
  6. Add business logic required for each method.
  7. 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.
  8. Add the log4net.config file and copy the code from the ExamplePeriodicService project. Be sure to set Copy always on the file properties and update the <file> value attribute in accordance with the comment in the log4net.config file. This configure sets up a rolling file appender and a console appender for log4net.
⚠️ **GitHub.com Fallback** ⚠️