Create a Windows service using Topshelf

Source: Internet
Author: User

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

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.