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.
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).
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).
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.
Run your service.exe install
to install the service.
See the Topshelf Command Line Reference documentation for more information.
cmd.exe
as administrator Navigate the console to
.\myConsoleApplication\bin\Release\
Run the command
.\myConsoleApplication.exe install
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"); }); } } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With