posted by Will Senn on Mon 5th Jan 2004 19:35 UTC
"Windows .NET Services, Page 3"

4. Rename the main Class

While the namespace has been changed to timedService, the name of the class is a generic - Service1, not descriptive and not helpful, we will change it. To do so, find the Class View window. It is usually in the top right window as a tab behind the Solution Explorer. If you cannot see the window anywhere, click the IDE menu item: View - Class View.

a. Expand the timedService Project by clicking the + sign to the left of the name.
b. Expand the timedService Code Namespace.
c. Right Click on the Service1 Code Class.
d. Click Properties. Find the Properties window, usually the bottom right window. If you cannot see the window anywhere, click the IDE menu item: View-Properties Window. Make sure that Service1 CodeClass is showing in the dropdown at the top of the properties window. Below that you will see a Misc property, expand it if it is not already.
e. Click the text field to the right of (Name)
f. Type in something like C_TimedService or whatever you like that helps you to remember that this is the Class that contains our service logic. You will see the changes once the focus changes from the text field, ie. Press enter, tab, click on another window, whatever. It is sad, but the change of name is not complete until we replace the name Service1 in the Main and Initialize Component methods.



g. Click back to timedService.cs code view.
h. Type Ctrl-H to bring up the Replace dialog or from the IDE main menu - select Edit - Find and Replace - Replace.
i. Type Service1 into the 'Find what' edit box
j. Type C_TimedService into the 'Replace with' edit box



k. Click the Replace All button - should result in 3 occurrence(s) replaced.
Now, let's flesh out the service by adding the ability to actually install and uninstall the service.


5. Add the Service Installer/Uninstaller

In order to actually be able to install our program as a service requires hooks into the Windows Service API and can be rather involved from a programmatic standpoint. You should be glad that the IDE abstracts the details away from the programmer so gracefully. This abstraction makes adding an installer, a piece of cake.

a. Click back to the timedService.cs [Design] window.
b. Right Click in the window.
c. Select Add Installer. The view will change to ProjectInstall.cs [Design] and there will be two icons added to the view, serviceProcessInstaller1 and serviceInstaller1.





d. Switch to the Properties Window - serviceInstaller1 System.ServiceProcess.ServiceInstaller should be selected.
e. Change the (Name) property to something like timedServiceInstaller
f. Change the ServiceName property to something like timedService - this is the display name that will show up in Administrative Tools - Services.



g. Select serviceProcessInstaller1 System.ServiceProcess.ServiceProcessInstaller from the Properties Window dropdown.
h. Change the (Name) property to something like timedServiceProcessInstaller.
i. Change the Account to LocalSystem by selecting it from the dropdown that will appear when you click to the right of Account.




The program is now installable and uninstallable as a service, however, the program does not really do anything, yet. We will fix that.

Table of contents
  1. "Windows .NET Services, Page 1"
  2. "Windows .NET Services, Page 2"
  3. "Windows .NET Services, Page 3"
  4. "Windows .NET Services, Page 4"
  5. "Windows .NET Services, Page 5"
e p (0)    31 Comment(s)

Related Articles

posted by Thom Holwerda on Fri 13th Jun 2008 18:09, submitted by wakeupneo
posted by Thom Holwerda on Fri 22nd Feb 2008 09:16, submitted by obsethryl