banner



How To Install Topshelf Windows Service Using Sc Commands

In the previous mail service, we have discussed how to create a windows service using the .internet with visual studio project template. In this post,  we will meet creating a windows service using the Topshelf library. Besides, Nosotros would discuss the reward of using Topshelf over using visual studio project template.

Windows Service with Topshelf

Objectives:

  • Why Topshelf.
  • Creating windows service using Topshelf
  • Installation/Uninstallation
  • Source code

Topshelf is an open up source framework library that helps to host the windows services adult in .net. Topshelf library helps the developer in multiple means, right from the evolution, debugging to the installation of the production.

Why Topshelf:

i.   Like shooting fish in a barrel Evolution:

Using Topshelf, a console application can exist hosted as windows service. It is e'er like shooting fish in a barrel to create a console app than creating a windows service using visual studio project template.

2.   Easy Debugging:

Windows service created using Topshelf tin be debugged every bit piece of cake as a panel app. We tin can debug the code just by pressing F5 and F10 as usual. You practise not demand to install windows service and attach the running procedure as needed by when we create windows service using a project template. Y'all can refer the debugging process for windows service in my previous mail service.

3.   Piece of cake Installation:

You practise not demand to know about "InstallUtil.exe" and its related dependency and commands. With Topshelf we merely need a simple control 'Production.exe install' and for uninstallation 'Product.exe uninstall'

four. Nuget Presence:

It can exist added in the project as the Nuget library.

Creating windows service in .net using Topshelf

Below are the steps to be followed:

i. Create new project -> Select Panel application.

Console application

2. Add Topshelf nuget library into your project.

  • RightClick  on your project
  • Choose "Manage NuGet Packages"
  • Search Topshelf in the NuGet Package window.
  • Click "Install"

Topshelf nuget

3. Add a class that contains the windows service logic such every bit what should be done on service start and service stopped.

Hither, Grade should contain 2 methods Start() and Stop() which will be wired up with the OnStart and OnStop events of windows service respectively. These methods are not mandatory to ascertain and wired upwards. Below form log the message to a log file on first and stop events.

          using System; using Organization.IO;  namespace WinServiceTopShelf {     public course OwnService     {                 public void            Start()         {             WriteToFile("Service started. Timestamp " + DateTime.Now);                    }         public void            Cease()         {             WriteToFile("Service stopped. Timestamp " + DateTime.Now);         }                 public void WriteToFile(cord Message)         {             string path = AppDomain.CurrentDomain.BaseDirectory + "\\LocalLog";             if (!Directory.Exists(path))             {                 Directory.CreateDirectory(path);             }             string filepath = AppDomain.CurrentDomain.BaseDirectory + "\\LocalLog\\Log_" + DateTime.Now.Date.ToShortDateString().Replace('/', '_') + ".txt";             if (!File.Exists(filepath))             {                 // Create a file to write to.                    using (StreamWriter sw = File.CreateText(filepath))                 {                     sw.WriteLine(Message);                 }             }             else             {                 using (StreamWriter sw = File.AppendText(filepath))                 {                     sw.WriteLine(Message);                 }             }         }     } }                  

4. Create a configuration file which has Topshelf configuration related to service information such every bit ServiceName, ServiceDescription and EventBinding[start and stop] etc.

          using Topshelf;  namespace WinServiceTopShelf {     internal class Configuration     {         internal static void Configure()         {             HostFactory.Run(configure =>             {                 configure.Service(service =>                 {                     service.ConstructUsing(s => new OwnService());                     service.WhenStarted(s => south.Start());                     service.WhenStopped(s => south.Terminate());                 });                 //Setup Account that window service use to run.                   configure.RunAsLocalSystem();                 configure.SetServiceName("WinServiceWithTopshelf");                 configure.SetDisplayName("WinServiceWithTopshelf");                 configure.SetDescription("WinService With Topshelf");             });         }     } }                  

five. Now, In program.cs file, call the Configure() method.

          public form Program     {         static void Main(string[] args)         {             Configuration.Configure();         }     }                  

6. Now window service is created. Y'all tin run this service by clicking Start button or pressing F5.

Installation/Uninstallation:

  1. Run CMD every bit administrator
  2. Set drive path to bin/debug folder where application.exe exists.
  3. execute this command for installation: <application>.exe install
  4. execute this control for Un-installation: <application>.exe uninstall

Sourcecode:

WinServiceSourceCode

  • External References: Topshelf Doc

Conclusion:

In this post, Nosotros have seen the reward of using topshelf for hosting the windows service developed in .cyberspace. Happy coding.

How To Install Topshelf Windows Service Using Sc Commands,

Source: https://techdiksha.com/create-a-windows-service-in-net-using-topshelf/

Posted by: estradaalif1955.blogspot.com

0 Response to "How To Install Topshelf Windows Service Using Sc Commands"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel