c# - how do i make application run as a service?
There is a tempate called "Windows Service" in visual studio. If you have any questions let me know, I write services all day long.
It amounts to this:
- Create a new "Windows Service" application in Visual Studio
- Port your application's source into the service's execution model, AKA your Main function becomes part of an event handler triggered by a timer object or something along those lines
Add a Service Installer class to your Windows Service project - it'll look something like this code snippet below:
[RunInstaller(true)] public partial class PollingServiceInstaller : Installer { public PollingServiceInstaller() { //Instantiate and configure a ServiceProcessInstaller ServiceProcessInstaller PollingService = new ServiceProcessInstaller(); PollingService.Account = ServiceAccount.LocalSystem; //Instantiate and configure a ServiceInstaller ServiceInstaller PollingInstaller = new ServiceInstaller(); PollingInstaller.DisplayName = "SMMD Polling Service Beta"; PollingInstaller.ServiceName = "SMMD Polling Service Beta"; PollingInstaller.StartType = ServiceStartMode.Automatic; //Add both the service process installer and the service installer to the //Installers collection, which is inherited from the Installer base class. Installers.Add(PollingInstaller); Installers.Add(PollingService); } }
Finally you'll use a command line utility to actually install the service - you can read about how that works here:
http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/d8f300e3-6c09-424f-829e-c5fda34c1bc7
Let me know if you have any questions.
There is the Open Source Framework that host .net application as Windows service. There are no pain installing, uninstalling windows service. It decouples very well. Please check this post Topshelf Windows Service Framework Post