Installing a Topshelf application as a Windows service

后端 未结 4 1757
离开以前
离开以前 2020-12-23 16:12

Using Visual Studio Express 2012, I\'ve created a console application using Topshelf (Version 3.1.107.0). The application works as a console application, but I can\'t figure

4条回答
  •  Happy的楠姐
    2020-12-23 16:24

    1. Start Visual Studio and create a new C# Console-Application
    2. Right click on references and go to manage NuGet-Packages
    3. Download and install Topshelf via NuGet
    4. Paste the Code below into your application and include all imports.
    5. Switch from “Debug” mode to “Release” and build the application.
    6. Run cmd.exe as administrator
    7. Navigate the console to

      .\myConsoleApplication\bin\Release\
      
    8. Run the command

      .\myConsoleApplication.exe install
      
    9. Run the command

      .\myConsoleApplication.exe start
      

    Code:

    using System;
    using System.Threading;
    using Topshelf;
    using Topshelf.Runtime;
    
    namespace MyConsoleApplication
    {
        public class MyService
        {
            public MyService(HostSettings settings)
            {
            }
    
            private SemaphoreSlim _semaphoreToRequestStop;
            private Thread _thread;
    
            public void Start()
            {
                _semaphoreToRequestStop = new SemaphoreSlim(0);
                _thread = new Thread(DoWork);
                _thread.Start();
            }
    
            public void Stop()
            {
                _semaphoreToRequestStop.Release();
                _thread.Join();
            }
    
            private void DoWork()
            {
                while (true)
                {
                    Console.WriteLine("doing work..");
                    if (_semaphoreToRequestStop.Wait(500))
                    {
                        Console.WriteLine("Stopped");
                        break;
                    }
                }
            }
        }
    
        public class Program
        {
            public static void Main()
            {
    
                HostFactory.Run(x =>                                 
                {
                    x.StartAutomatically(); // Start the service automatically
    
                    x.EnableServiceRecovery(rc =>
                    {
                        rc.RestartService(1); // restart the service after 1 minute
                    });
    
    
                    x.Service(s =>
                    {
                        s.ConstructUsing(hostSettings => new MyService(hostSettings));
                        s.WhenStarted(tc => tc.Start());             
                        s.WhenStopped(tc => tc.Stop());               
                    });
                    x.RunAsLocalSystem();                            
    
                    x.SetDescription("MyDescription");        
                    x.SetDisplayName("MyDisplayName");                       
                    x.SetServiceName("MyServiceName");    
    
                });                                                 
            }
        }
    }
    

提交回复
热议问题