Topshelf is an open-source, cross-platform service framework that supports Windows and mono, and allows you to build a service that is easy to use with just a few lines of code. Official website: http://topshelf-project.com
Here is a common console built into Windows services:
1. Referencing Assembly TopShelf.dll
Download via NuGet installation or official website
2. Create a Apimain class
public class Apimain {public apimain () { //Initialize method, can not } public void Start () { /// Service startup, execution content } public void Stop () { //service stopped, content executed } }
3. Rewriting Program.cs
public class program { static void Main (string[] args) { hostfactory.run (x = { X.service <APIMain> (s + = { s.constructusing (name = new Apimain ()); s.whenstarted (TC = TC). Start ()); Start the service execution method s.whenstopped (TC = TC. Stop ()); Stop pre-Service execution Method }); X.runaslocalsystem (); X.setdescription ("Data Interface Service"); Service Description Name x.setdisplayname ("Data Interface Service");//service Display name x.setservicename ("Elegantwebapiserver");} ); }
4.cmd command to install or uninstall a service
Installation: XXX.exe install
Uninstall: XXX.exe uninstall
Create a Windows service using Topshelf