Create a Windows service using Topshelf

Source: Internet
Author: User

This approach features: code simple, open source components, Windows services can run multiple instances

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

1th step: Referencing Assemblies TopShelf.dll and Log4net.dll

2nd step: Create a service class MyClass, which contains two methods start and stop, also includes a timer timer, every 5 seconds to the text file to write characters

public class MyClass {readonly Timer _timer;        private static readonly String FileName = directory.getcurrentdirectory () + @ "\" + "test.txt";                Public MyClass () {_timer = new timer () {AutoReset = True,            Enabled = true}; _timer. Elapsed + = Delegate (object sender, Elapsedeventargs e) {this.witre (string.            Format ("Run DateTime {0}", DateTime.Now));        };            } void Witre (String context) {StreamWriter SW = File.appendtext (FileName); Sw.            WriteLine (context); Sw.            Flush (); Sw.        Close (); } public void Start () {this.witre (string.        Format ("Start DateTime {0}", DateTime.Now)); } public void Stop () {this.witre (string.        Format ("Stop DateTime {0}", DateTime.Now) + Environment.NewLine);   } } 

3rd Step: Use Topshelf to host our service, mainly topshelf how to set up our service configuration and method call when start and stop

Class program    {        static void Main (string [] args)        {            Hostfactory.run (x = =            {                x.service<my Class> ((s) =                {                    s.setservicename ("Ser");                    S.constructusing (name = new MyClass ());                    S.whenstarted ((t) = T.start ());                    S.whenstopped ((t) = T.stop ());                );                X.runaslocalsystem ();                Description of the service                x.setdescription ("topshelf_description");                The display name of the service                x.setdisplayname ("Topshelf_displayname");                Service name                x.setservicename ("Topshelf_servicename");}            );        }    

4th Step: cmd command

Consoleapp_topshelf.exe Install (installing Windows Services)

Consoleapp_topshelf.exe Uninstall (uninstall Windows service)

Code Download: Http://files.cnblogs.com/aierong/ConsoleApp_Topshelf.rar

Create a Windows service using Topshelf

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.