Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Installing a Topshelf application as a Windows service

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 out how to install it as a service. I've published the project from within Visual Studio (Build, Publish), started a command prompt as Administrator, navigated to the folder where the application was published, and run setup.exe -install from the command prompt. The application is installed and runs, but as a console application, not a Windows service. What am I missing here?

For those who may not be familiar with Topshelf, it is a Windows Service framework for .Net and is supposed to facilitate the scenario I describe above - develop and debug as a console application, deploy as a Windows Service. See the documentation at http://docs.topshelf-project.com/en/latest/index.html.

like image 213
Brendan Reynolds Avatar asked Aug 13 '13 10:08

Brendan Reynolds


People also ask

Can we install console application as Windows service?

To run as a service you need to install it first (using ServiceInstaller class - see MSDN link above - or installuitil.exe), and the run the service from the control panel. ServiceInstaller is just a utility class to deal with Windows services (a little bit like installutil.exe or sc.exe utilities).

What is a topshelf service?

Topshelf is a Windows service framework for the . NET platform. Topshelf makes it easy to create a Windows service, test the service, debug the service, and ultimately install it into the Windows Service Control Manager (SCM).

What is the use of topshelf?

Topshelf is an open source library freely available on GitHub. The project's Web site has documentation and examples to help. The Topshelf project allows you to take an existing console application and turn it into a Windows Service, while still handling the various events such as Start and Stop.


2 Answers

Run your service.exe install to install the service.

See the Topshelf Command Line Reference documentation for more information.

like image 177
Travis Avatar answered Oct 13 '22 10:10

Travis


  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<MyService>(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");                  });                                                          }     } } 
like image 42
Jonas_Hess Avatar answered Oct 13 '22 10:10

Jonas_Hess