1.Topshelf is another way to create a Windows service, Topshelf is an open-source, cross-platform hosting service framework that supports Windows and mono and can build a convenient service host with just a few lines of code.
2. Add Reference: Topshelf.dll
Topshelf.Log4Net.dll
3. Latest Version: Https://github.com/Topshelf/Topshelf/downloads
4. Official Case:
classProgram { Public Static voidMain () {Hostfactory.run (x=//1{X.service<TownCrier> (s =//2{s.constructusing (name=NewTowncrier ());//3s.whenstarted (TC = TC). Start ());//4s.whenstopped (TC = TC). Stop ());//5 }); X.runaslocalsystem (); //6X.setdescription ("Sample topshelf Host");//7X.setdisplayname ("Stuff");//8X.setservicename ("Stuff");//9});//Ten } } Public classTowncrier {ReadOnlyTimer _timer; PublicTowncrier () {_timer=NewTimer ( the) {AutoReset =true }; _timer. Elapsed+ = (sender, EventArgs) = Console.WriteLine ("It's {0} and all are well", DateTime.Now); } Public voidStart () {_timer. Start (); } Public voidStop () {_timer. Stop (); } }
5. Safety Loading method:
Installation: TopshelfDemo.exe Install
Start: TopshelfDemo.exe start
Uninstall: TopshelfDemo.exe Uninstall 6. When the installation is complete, you can see it in the Windows service.
Create a Windows service using Topshelf