Installing a Service

I l @ ve RuBoard

A Windows service must be registered with the system and the SCM before it can be started or debugged . (We'll talk about debugging a service in the next section.) The .NET Framework ships with the InstallUtil tool, which helps you quickly and easily install a Windows service.

The first step in the process is to add a ServiceInstaller class to the Windows service project. An easy way to do this is to click the Add Installer link at the bottom of the Properties dialog box for the Windows service component. You end up with a bare-bones ProjectInstaller class, which is added to your existing Windows service project. The ProjectInstaller class has the RunInstaller ( True ) attribute defined, which is what the InstallUtil.exe utility looks for when it attempts to invoke the Install or Uninstall method.

Before you run the InstallUtil application, you need a separate ServiceInstaller component for every service in the application. You'll want to change several properties for each ServiceInstaller component. The Properties dialog box for the component allows you to specify the display name, startup type, and dependent services and give the service a name . Looking at the designer-generated code, you can see that it simply adds these component instances to the System.Configuration.Install.InstallerCollection at run time to start the installation process.

The ServiceInstaller.DisplayName name will show up as the friendly name in the SCM. The ServiceName property should have the same name as the class that inherits from ServiceBase because when the service is installed, an EventLog source with that name is created in the Windows application event log. The ServicesDependedOn property allows for an array of strings ”clicking the button next to this entry will allow you to define the set of services that this service is dependent on. Finally, the StartType of the service can have a value of Automatic , Manual , or Disabled , which will determine the behavior of the service when the machine boots up. Figure 7-6 shows the Properties dialog box for the ServiceInstaller class.

Along with the ServiceInstaller component comes a ServiceProcessInstaller component, which is needed for each assembly that contains Windows services. For each instance of a ServiceProcessInstaller , you can specify that a service application is run with specific account credentials other than the credentials of the logged-on user . This is done by specifying a username and password combination and setting the Account property to user . If the username and password are Nothing, at installation time a dialog box will appear prompting you for them. You can also set the Account property so the service will run under the computer's System account or a local/network service account.

Figure 7-6. The ServiceInstaller Properties dialog box.

graphics/f07pn06.jpg

The following code is the designer-generated code for the InitializeComponent method of the ProjectInstaller class. This code is essentially all there is to creating an installer for a service.

 FriendWithEventsServiceInstaller1AsServiceProcess.ServiceInstaller <System.Diagnostics.DebuggerStepThrough()>_ PrivateSubInitializeComponent() Me.ServiceProcessInstaller1=_ NewServiceProcess.ServiceProcessInstaller() Me.ServiceInstaller1=NewSystem.ServiceProcess.ServiceInstaller() ' 'ServiceProcessInstaller1 ' Me.ServiceProcessInstaller1.Account=_ ServiceProcess.ServiceAccount.LocalSystem Me.ServiceProcessInstaller1.Password=Nothing Me.ServiceProcessInstaller1.Username=Nothing ' 'ServiceInstaller1 ' Me.ServiceInstaller1.DisplayName= "TimeService" Me.ServiceInstaller1.ServiceName= "TimeService" Me.ServiceInstaller1.StartType=_ ServiceProcess.ServiceStartMode.Automatic ' 'ProjectInstaller ' Me.Installers.AddRange(_ NewSystem.Configuration.Install.Installer()_ {Me.ServiceProcessInstaller1,Me.ServiceInstaller1}) EndSub 

The Installer class, which is a base class for the ServiceInstaller , has built-in transactional support for rolling back installations that fail and thus resetting registry and other settings to their previous values. It exposes methods for installing, rolling back, and uninstalling applications as well as several methods for defining public events that can be handled to customize the installation of your service. These include AfterInstall , AfterRollback , AfterUninstall , BeforeInstall , BeforeRollback , BeforeUninstall , Committed , and Committing .

The InstallUtil application takes several command-line parameters, but simply accepting the defaults yields the output for the TimeService executable shown in Figure 7-7.

Figure 7-7. The InstallUtil.exe output.

graphics/f07pn07.jpg

I l @ ve RuBoard


Designing Enterprise Applications with Microsoft Visual Basic .NET
Designing Enterprise Applications with Microsoft Visual Basic .NET (Pro-Developer)
ISBN: 073561721X
EAN: 2147483647
Year: 2002
Pages: 103

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