Deploying a Windows Service

After you create a Windows service with Visual Studio .NET, you need to deploy it. You can use a Framework utility called the .NET Framework Installation utility (InstallUtil.exe) or a Windows Installer file to install or uninstall a Windows service. This section walks you through both processes. (You could also just set the proper Registry keys if you wanted, though this is not as easy.)

Using the Installation Utility

The Installation utility executes the installers that are contained in the Windows service’s .NET assembly. The installers can be turned on and off via the RunInstaller attribute. When the attribute is set to True, the installer will be executed. Setting it to False will disable the installer after a recompile of the project. The following code snippet shows the creation of the installer class called ProjectInstaller and the RunInstaller attribute:

<RunInstaller(True)> Public Class ProjectInstaller     Inherits System.Configuration.Install.Installer

Installers contain the code necessary to update the Windows Registry with appropriate information for the application. In the case of a Windows service application, they update the Registry based on the properties of two classes:

ServiceProcessInstaller The ServiceProcessInstaller class encapsulates the functionality necessary for all services. It is used by the installation utility ( InstallUtil.exe or Windows Installer) to write entries to the Registry. There is only one instance of this class per assembly.

ServiceInstaller The ServiceInstaller class updates the HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services subkeys in the Registry. There is an instance of this class for each service that might be included in the assembly.

These classes contain properties, methods, and events that let you set the values that will be written to the Registry or control what happens when the service is installed, committed, rolled back, or uninstalled as part of the install process.

After you create the Installer classes, you need to add them to the Installer collection of the InstallerComponent class. For example, the following code snippet updates the Registry with the service account information:

'ServiceProcessInstaller1 ' Me.ServiceProcessInstaller1.Password = "p@ssw0rd" Me.ServiceProcessInstaller1.Username = "servacct" 'ServiceInstaller1 ' Me.ServiceInstaller1.ServiceName = "MyServiceName"
Note 

The ServiceInstaller.ServiceName and the ServerBase.ServiceName (set in your service's code) need to be the same because the ServiceInstaller uses this name to locate the service in the assembly.

You then add the Installer classes to the Installer collection in the System.Configuration.Install.Installer class, as the following code snippet shows:

Me.Installers.AddRange(New _   System.Configuration.Install.Installer() _   {Me.ServiceProcessInstaller1, Me.ServiceInstaller1})
Note 

If you set the password and username to Nothing for the service account, you will be prompted for this information during the install of the Windows service.

For a simple component, you might not even override any of the methods on the Installer class because defaults are usually sufficient for installing services.

Note 

The Installer utility works in a transacted manner, so if the install for one assembly fails, all the assemblies listed will fail.

You can create your own installer by using these classes, as Listing 10.1 shows.

Listing 10.1: Using the ServiceInstaller and ServiceProcessInstaller Classes to Create an Installer

start example
Imports System Imports System.Collections Imports System.ServiceProcess Imports System.ComponentModel ' Set the RunInstallerAttribute to True to enable the installer <RunInstallerAttribute(True)> _ Public Class ProjectInstaller Inherits System.Configuration.Install.Installer Private pi As ServiceProcessInstaller Private si As ServiceInstaller Public Sub New() ' Create instances of the installers pi = New ServiceProcessInstaller() si = New ServiceInstaller()          ' Run this service under the local system account, you could specify ' the username and password properties to set this to a domain account pi.Account = ServiceAccount.LocalSystem ' The services will be started manually. si.StartType = ServiceStartMode.Manual        ' ServiceName must equal those on ServiceBase derived classes. si.ServiceName = "My Service"          ' Add the installers to the collection, order does not matter. Installers.Add(si) Installers.Add(pi) End Sub End Class
end example

Visual Studio .NET makes it easy to create an installer for a service. When you are in Design view of the service, just right-click on a blank area of the Visual Designer screen and choose Add Installer from the pop-up menu, as shown in Figure 10.13. This will generate a new class called ProjectInstaller that will contain a ServiceProcessInstaller and ServiceInstaller classes.

click to expand
Figure 10.13: Adding an installer to a Windows Service in Visual Studio .NET

In Exercise 10.2, you will create a simple service and then create a project installer that you will explore. Finally you will use the InstallUtil.exe utility to install and uninstall the service.

Note 

Windows services run only on Windows NT–based operating systems such as Windows 2000 and Windows XP. The exercises dealing with Windows services, COM+, and remoting through IIS section will not work on Windows 9x or Windows ME. You can, however, go through the motions and see the options in a Windows Installer project or look at the code involved in generating an installer. You cannot, however, install and test the service.

Exercise 10.2: Installing a Windows Service

start example
  1. Create a new project by choosing File Ø New Ø Project from the main menu.

  2. Select Visual Basic Projects from the Project Types and choose Windows Service from the Templates.

  3. Name the project TimerService and click the OK button.

  4. Select the Toolbox toolbar and click on the Components section.

  5. Drag a Timer and EventLog component to the Service1.vb [Design] window.

  6. Right-click the Timer1 component and choose Properties from the pop-up menu.

  7. Set the Interval property to 5000. This is the number of milliseconds that the timer will wait. In this case, you are going to log a message to the Windows event log every 5 seconds.

  8. Right-click EventLog1 and choose Properties.

  9. Set the Log property to Application and the Source property to Service1.

  10. Right-click the Service1.vb [Design] window and choose View Code from the pop-up menu.

  11. In the OnStart() method for the Windows service, add the following code:

    Timer1.Start()
  12. In the OnStop() method for the Windows service, add the following code:

    Timer1.Stop()
  13. In the Class Name drop-down list box (the list box on the top left), choose Timer1.

  14. In the Method Name drop-down list box (the list box on the top right of the source window), choose Elapsed to add a Timer1_Elapsed event handler to your code.

    click to expand

  15. Add the following code to the Timer1_Elapsed event handler:

    EventLog1.WriteEntry("Your time is up, logging!")
  16. Switch back to the Service1.vb [Design] window, right-click on a blank area, and choose Add Installer.

  17. Right-click on a blank spot of the ProjectInstaller.vb [Design] window and choose View Code.

  18. Expand the Component Designer Generated Code region to reveal the installer code. You are looking at the components that create a ServiceProcessInstaller and ServiceInstaller.

  19. Scroll down until you locate the following code:

    Me.ServiceProcessInstaller1.Password = Nothing Me.ServiceProcessInstaller1.Username = Nothing 
  20. Set the Username and Password properties to an account that has local administrative rights so that the service can write to the Registry. (Normally you don’t want your service running as an account with administrative rights. However, we don’t want to focus on setting up security in this exercise.)

  21. Build the solution by choosing Build Ø Build Solution from the main menu.

  22. Launch a Visual Studio .NET command prompt by choosing Start Ø Programs Ø Microsoft Visual Studio .NET Ø Visual Studio .NET Tools Ø Visual Studio .NET Command Prompt.

  23. Use the InstallUtil.exe utility to install the service by typing the following at the command prompt:

    installutil "C:\Documents and Settings\your_username\My Documents\Visual Studio~CA Projects\TimerService\bin\TimerService.exe" 

    The path should be the path to the executable that you compiled.

  24. You should get a successful install message. If you get an error, you probably have a typo in the username or password.

  25. Test the service by going to the Service Controller applet in Start Ø Settings Ø Control Panel Ø Administrative Tools Ø Services.

  26. Find Service1 in the list of services and right-click it. Choose Start from the pop-up menu.

  27. Wait about 10 or 15 seconds and then stop the service.

  28. Open the Event Viewer tool by choosing Start Ø Settings Ø Control Panel Ø Administrative Tools Ø Event Viewer. You should see a message from the Service1 source that says, “Your time is up!”

  29. Uninstall Service1 by typing the following at a Visual Studio .NET command prompt:

    installutil  /u "C:\Documents and Settings\your_username\My Documents\Visual~CA Studio Projects\TimerService\bin\TimerService.exe" 

  30. Save this project because you will use it in the next exercise.

end example

Using the Windows Installer

A better way to distribute your service into production environments is to use a Windows Installer project. This project can be used to install and uninstall the application (much like InstallUtil.exe) but can also benefit from being able to be pushed out via software policies in Active Directory and is the standard way to install software on the Windows platform so administrators and users will be familiar with it.

You can create a Windows Installer project by adding the primary output of the project that you used to create your service. You then need to add the project output for the service to the Custom Actions Editor, as shown in Figure 10.14.

click to expand
Figure 10.14: Adding the project output to the Custom Actions Editor

The Windows Installer project will then use the code generated by the ServiceProcessInstaller and the ServiceInstaller classes to instruct the Windows Installer project on how to install, commit, roll back, or uninstall the Windows service.

In Exercise 10.3, you will create a Windows Installer project for the simple service created in Exercise 10.2 and install and uninstall the service.

Exercise 10.3: Creating a Windows Installer Project to Install the Service

start example
  1. Open the TimerService project if it is not already open.

  2. Add a new project to the solution by right-clicking Solution ‘TimerService’ and choosing Add Ø New Project from the pop-up menu.

  3. Select Setup And Deployment Projects in the Project Type window and select Setup Project in the Template window.

  4. Type TimerInstall for the project name and click the OK button.

  5. Right-click the project name, TimerInstall, and choose Add Ø Project Output.

  6. Choose Primary Output from the Add Project Output dialog box and click OK.

  7. Switch to the Custom Actions Editor by right-clicking the setup project name (TimerInstall) and choosing View Ø Custom Actions.

  8. Right-click Custom Actions and choose Add Custom Action from the pop-up menu.

  9. Select the Application Folder in the dialog box and then double-click the Primary Output From TimerService to add the actions to each of the events.

    click to expand

  10. Build your setup project by choosing Build Ø Build TimerInstall from the main menu.

  11. Test the install of the service by navigating to the \bin directory for TimerInstall and double-clicking the TimerInstall.msi file.

  12. Verify that the service is registered with the Service Controller applet by choosing Start Ø Settings Ø Control Panel and then double-clicking the Service Controller applet.

  13. Uninstall the service by using Add/Remove programs in Control Panel.

  14. Verify that the service is removed from the Service Controller applet.

end example



MCAD/MCSD(c) Visual Basic. NET XML Web Services and Server Components Study Guide
MCAD/MCSD: Visual Basic .NET XML Web Services and Server Components Study Guide
ISBN: 0782141935
EAN: 2147483647
Year: 2005
Pages: 153

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net