Create and Install .Net Core application as windows service using Quartz and Topshelf

This article will guide you through the necessary steps creating windows service. Whenever there is a need for some automated jobs that have to run On-Prem for a client, for many of us default choice has been windows service using Quartz and Topshelf.

Topshelf

Topshelf can make the creation of windows service easy by providing the ability to run as console applications and easily deploying as service. To set up a service, you need to modify Program.cs with some setup code for creating windows services and setting some service metadata.

Quartz.Net

We need the ability to schedule jobs within the SchedulerService hence inject an ISchedulter from Quartz.NET

Let's get started creating one.

Create a Console App (.NET Core)


Install necessary Nuget packages.

After installing NuGet packages we need to inject Quartz and Topshelf, scheduler classes.

Modify Program.cs class to use the top shelf to inject Quarts Service Scheduler. In Program.cs you can specify Service Description, Service Display Name & Service Name. I am using App.config to store those details.

Program.cs

Schedular.cs is used to schedule Jobs you can specify TriggerName, JobName and a schedule for your Job in this example this service is scheduled to run every minute. the schedule can be set using CRON expression.

Lastly, you need to create job Inheriting from the IJob Quartz interface.

SampleJob just logs execution time in console. you can leverage Job according to business needs.

Running job will result in running console application every minute which logs time to console as below.


Installing Console app as windows service.

Before installing the console app we need to create .exe for the console app, you can follow below steps to create. you need to right-click on the project and click on Publish.


Once you click on Publish will see window as below where you need to do publish configuration by clicking on Configure


Once you click on configure you need to select Deployment Mode equals to Seld-contained and click Publish


Once you publish you will be able to see BloggingService.exe file in the published folder


Now we have successfully create .exe file next step is to install this as service using Command Line Interface.

Press Windows + R key and type cmd to open the command-line interface. 
Note: You need to run as Administrator.

Navigate to a folder there we have created .exe file.


Once you navigate to the folder you can use BloggingService.exe install command to install service.


To verify you can check Services to see if  "LoggingScheduler" is installed. you can use Services.msc to open the Services window.


To uninstall you can use BloggingService.exe uninstall


I hope this helps you..!


Post a Comment

0 Comments