Working with Windows Service using Visual Studio
This article explains the purpose of Windows Service. It also shows how to create Windows Service using Visual Studio 2008, Add installer and install it. It also tells how to install it using Command line. It also explains you to manage Windows Service using Computer Management like start, stop and pause the service.
WINDOWS SERVCIE
Introduction
Windows Service is a long running executable application that runs in their own Windows sessions. These services can be automatically started when the computer boots, can be paused and restarted, and do not show any user interface. Windows services can be configured to start when the operating system is booted and run in the background as long as Windows is running, or they can be started manually when requiredCreate a Windows Service
Follow the below steps to a create Windows Service
Protected override void OnStart(string[] args)
{
EventLog.WriteEntry("Service", "WindowsService1 Started",
EventLogEntryType.Information);
}
Protected override void OnStop(string[] args)
{
EventLog.WriteEntry("Service", "WindowsService1 Stopped",
EventLogEntryType.Information);
}Create an Installer for your Windows Service
Private void InitializeComponent()
{
this.serviceProcessInstaller1 = new
System.ServiceProcess.ServiceProcessInstaller();
this.serviceInstaller1 = new
System.ServiceProcess.ServiceInstaller();
this.serviceProcessInstaller1.Account =
System.ServiceProcess.ServiceAccount.LocalSystem;
this.serviceProcessInstaller1.AfterInstall += new
System.Configuration.Install.InstallEventHandler
(this.serviceProcessInstaller1_AfterInstall);
this.serviceInstaller1.ServiceName = "Service1";
this.serviceInstaller1.StartType =
System.ServiceProcess.ServiceStartMode.Automatic;
this.Installers.AddRange(new System.Configuration.Install.Installer[]
{this.serviceProcessInstaller1,
this.serviceInstaller1});
}Install and run the Service from Command Line
Windows service can be installed from command line without creating the installer. Follow the below steps to install windows service from command line.
Installutil d:\documents\Visual Studio 2008\Projects\WindowsService1\bin\debug\WindowsService1.exe
Installutil \u d:\documents\Visual Studio 2008\Projects\WindowsService1\bin\debug\WindowsService1.exeStart and Stop the service
Go to Computer Management to start and stop the service. Follow the below steps:Managing Services
The installed service can be managed by launching "Services" from the windows Control Panel --> Administrative Tools or by typing "Services.msc" in the run command. In Windows Vista and later, the services can be managed from the Services tab in Task Manager. The "Services" management console provides a brief description of the service functions and displays the path to the service executable, its current status, startup type, dependencies and the account under which the service is running. It enables users toAllow Service to Interact with Desktop
The Windows Service does not have a user interface. But the developers can add the Form and other UI Components to the service. Follow the below steps to add user interface to the Windows Service:
When the User Interface is added to the Windows Service, then it can be interacted by any logged in user. So it may lead to some security risk.Security Context for Services
Services run in the context of the default system account, called LocalSystem, which gives them different access privileges to system resources than the user. You can change this behavior to specify a different user account under which your service should run. The account property for the process allows you to set the service to one of four Account types:
Follow the below steps to set the Account Types:Debug Windows Service Applications
Debugging a service is not as straightforward as debugging other visual studio application types. To debug a service, we must start the service and then attach a debugger to the process in which it is running. You can then debug your application using all of the standard debugging functionality of Visual Studio.
Follow the below steps to debug a service:Drawbacks of attaching a debugger
Log Information about Services
All Windows Service projects have the ability to interact with the Application event log and write information and exceptions to it. AutoLog property is used to enable this functionality in your application. By default, logging is turned on for any service that is created with the Windows Service project template