. . Hong Kong Software Company
software house
Expert in web-based solutions
Software House in Hong Kong

Microsoft Certified Solution Developer

 

Chief Architect's Blog on software development

8 Aug 2007
Windows Service
Windows Service is very useful for many server application, e.g. MS SQL Server, IIS Server, antivirus software.   Windows Service's lifetime is controlled by Service Control Manager. The lifetime of a Windows Service can be controlled through Service Control Manager (SCM) which can be accessed from Control Panel > Administrative Tools > Services.  You can configure to start a windows service automatically when the computer starts.  If you are developing self-host WCF service, Windows Service is a practical solution in production environment.  Console-based or Form-based solution is only practical in testing envioronmnet.
 
To create Windows Service in Visual Studio 2005 or 2008, you can:
  1. Create a Windows Service Project using the Windows Service template.
  2. Rename the filename and ServiceName property to an appropriate name.
  3. Add an installer class to that project.  You can also right-click the design surface of your Windows Service Project and select "Add Installer".
  4. A project installer (containing ServiceProcessInstaller1 and ServiceInstaller1) is added to the project automatically.
  5. In the property of ServiceProcessInstaller1, change the Account Property (e.g. to NetworkService) as appropriate.
  6. Change the DisplayName, Description, ServiceName and StartType property of ServiceInstaller1 to appropriate properties.
  7. Write code in the onStart and onStop event handler.
  8. Other important event handlers, e.g. OnPause, OnContinue, OnShutdown, OnSessionChange, OnPowerEvent and OnCustomCommand.
  9. Optionally, you can add a timer (System.Timers.Timer not the window form timer to the service file design view.  If you see Tick event instead of Elapse event, you get the wrong timer) and add a onElapsedTime event handler.
  10. When the project is built, it will create an executable at <projectname>\obj\debug\<yourWindowsServiceFile.exe> location.
  11. After that, you need to deploy it to the server.  This is done by an InstallUtil.exe file which is usually located in C:\Windows\Microsoft.Net\Framework\v2.0.50727.  The syntax is: InstallUtil.exe <yourWindowsServiceFile.exe>. If there are dll files associated with your Windows Service, you need to put them in the same folder of your Windows Service Executable file.
  12. If you are developing Windows Services on .Net 1.1, you need to use tools appropriate for .Net 1.1.
  13. You can use the Service Control Manager to control starting or stopping the service.
  14. For uninstalling the service, use this command: InstallUtil.exe /u <yourWindowsServiceFile.exe>.


Member Log in
User ID:
Password:
 
 
Hong Kong Software House



 
March On to the Information Era. Realize your Vision via Customized Software.