Deploy your Job with Topshelf

quote

Create a new console program and use Nuget to add a reference to Topshelf. Program installation instructions:

  • Install-Package Topshelf

 

configure

Here we add a ServiceMain class that contains a constructor, service start, stop, terminate, continue methods:

public class SerivceMain
    {
        public SerivceMain()
        {
       //add you code
        }

        public void Start()
        {
       //add you code
        }

        public void Stop()
        {
        //add you code
        }

        public void Pause()
        {
         //add you code
        }

        public void Continue()
        {
          //add you code
        }
    }

Register several behaviors of the class just written in the Main function:

 1 HostFactory.Run(x =>
 2             {
 3                 x.Service<ServiceMain>(s =>
 4                 {
 5                     s.ConstructUsing(name => new ServiceMain());
 6                     s.WhenStarted(tc => tc.Start());
 7                     s.WhenStopped(tc => tc.Stop());
 8                     s.WhenPaused(tc => tc.Pause());
 9                     s.WhenContinued(tc => tc.Continue());
10                 });
11                 x.RunAsLocalService();
12 
13                 x.SetDescription("后台服务");
14                 x.SetDisplayName("SerivceDisplayName");
15                 x.SetServiceName("ServiceName");
16             });
View Code

you're done

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324698289&siteId=291194637