Installing a Windows Service

   


Installing a Windows Service

The .NET Framework allows you to create custom installer components. The custom installer components are the classes that can be used by an installation tool such as installutil.exe to properly install an application.

The following three installation components are used when creating an installer for the Windows service applications:

  • The System.ServiceProcess.ServiceProcessInstaller Class This class creates the records for all the Windows services contained with the application in the Windows service database. You need to have just one instance of the ServiceProcessInstaller class in a Windows service application.

  • The System.ServiceProcess.ServiceInstaller Class This class writes specific information about a Windows service in the Windows service database. You'll have as many instances of the ServiceInstaller class as the number of services in a Windows service application.

  • The System.Configuration.Install.Installer Class This class works as the entry point for the installation process. The Installer class maintains a collection of custom installer objects, which can be accessed through the Installer property. You'll add the instance of ServiceProcessInstaller class and instances of the ServiceInstaller class corresponding to your Windows service application to this collection. You'll also apply the RunInstallerAttribute attribute to this class and set its value to true.

When you use an installation tool such as installutil.exe to install a Windows service application, the installation tool takes the following steps:

  1. Looks for an Installer class in the Windows service application that has its RunInstallerAttribute set to true and creates an instance of this class.

  2. Gets a collection of custom installer objects from the Installer property and invokes the Install() method on each of these objects to perform the custom installation.

  3. Rolls back the installation of all custom installers if any one of them failed.

The ServiceProcessInstaller and the ServiceInstaller Classes

I'll talk more about the installation process and the Install class later in Chapter 10. In this section, I'll focus on some important properties of the ServiceProcessInstaller and the ServiceInstaller classes because these properties specify the values, which are written to the Windows service database.

The ServiceProcessInstaller class specifies the security settings for the Windows services in the Windows service application. Table 6.3 lists some important properties of the ServiceProcessInstaller class.

Table 6.3. Important Members of the ServiceProcessInstaller Class

Property

Description

Account

Specifies the type of account under which the services run. This property takes one of the values from the ServiceAccount enumeration. These values are LocalService , LocalSystem , NetworkService , and User .

If you set the Account property to User and do not specify a value for either the Username or Password property, you are prompted to enter both username and password at the time of installation.

Password

This property is only useful when the Account property is set to User . In that case, this property specifies the password of the account under which the service application runs.

Username

This property is only useful when the Account property is set to User . In that case, this property specifies the username of the account under which the service application runs.

The ServiceInstaller specifies the individual settings for each service in the Windows service database. Table 6.4 lists some important properties of the ServiceInstaller class.

Table 6.4. Important Members of the ServiceInstaller Class

Property

Description

DisplayName

Specifies a descriptive friendly name that identifies the service to the user. This name is often used by interactive tools to display the service to the user.

ServiceName

Specifies a unique name by which the system identifies this service.

The ServiceName must be between 1 and 256 characters, and cannot contain forward slash, backslash, or control characters .

ServicesDependedOn

Specifies an array of strings representing the services that must be running for this service to run. If any service in the array is not running, the SCM tries to start that service before starting this service.

StartType

Indicates how and when this service is started. The value of this property is one of the ServiceStartMode enumeration values Automatic , Disabled , or Manual . The value Automatic specifies that the service is started automatically when Windows starts. The default value of this property is Manual , which means that the service will not start automatically with Windows.

EXAM TIP

The ServiceName Property The ServiceName property of a ServiceInstaller object must exactly match the ServiceName property of the corresponding Windows service class.


NOTE

Windows Service Description Unfortunately, the Installer class does not have a property to set the description for a Windows Service. If you need to set a description for a service, you have two choiceseither use the Service Control utility (sc.exe) or modify the registry key corresponding to the Windows service, add a string value named Description , and then set the data for this value with the description text.


Adding Installer Classes to a Windows Service Project

When you use Visual Studio .NET to create a Windows service application, it is easy to create the required installer classes. The Properties window of the Windows service, as shown in Figure 6.4, displays a link called Add Installer. When you click on that link, Visual Studio .NET automatically adds a class named ProjectInstaller in your project. This class derives from the Installer class and contains an instance of the ServiceProcessInstaller class and one or more instances of the ProcessInstaller class (depending on the number of Windows services you have in the application).

Step By Step 6.2 demonstrates how to add Installer classes to a Windows service project.

STEP BY STEP

6.2 Creating an Installer for the Windows Service

  1. Open the project StepByStep6-1 . Access the design view for the OrderService component. Click anywhere on the background of the designer to select the service itself, rather than any of its components. Access the properties window for the service. In the gray area below the list of properties, you'll see a hyperlink named Add Installer (see Figure 6.4). Click on the hyperlink.

  2. The preceding step adds a component named ProjectInstaller.vb to the StepByStep6_1 project. Access the design view for ProjectInstaller.vb , and you'll see that it has two components ServiceProcessInstaller1 and ServiceInstaller1 .

  3. Access the properties for the ServiceProcessInstaller1 component; you'll note that this component is an instance of the ServiceProcessInstaller class. Change its Account property from User to LocalSystem .

  4. Access the properties for the ServiceInstaller1 component; you'll note that this component is an instance of the ServiceInstaller class. Change its DisplayName and ServiceName properties to OrderService and its StartType property to Automatic .

  5. Build the project StepByStep6_1 . The Windows service is now ready to be installed.

At this stage, the OrderService application has both a Windows service and an installer.

Using installutil.exe to Install a Windows Service Application

An executable file that has the code for the Installer class can be easily installed using the command-line installutil.exe tool that comes as a part of .NET Framework SDK.

Step By Step 6.3 demonstrates how to use installutil.exe to install a Windows service application in the Windows service database.

STEP BY STEP

6.3 Using installutil.exe to Install a Windows Service Application

  1. Open the Visual Studio .NET Command Prompt by selecting Start, Programs, Microsoft Visual Studio .NET, Visual Studio .NET Tools, Visual Studio .NET Command Prompt. This opens a command window with necessary environment variables set to run the .NET Framework SDK tools. Change the directory to the bin directory of the StepByStep6-1 project where the EXE file for the Windows service application is stored.

  2. Issue the following command; you see the results as shown in Figure 6.6.

     installutil StepByStep6-1.exe 
    Figure 6.6. Installing a Windows service application.

The Windows service application is now stored in the Windows service database. Not only this, but based on the information in the executable file, installutil.exe has also installed an event source named OrderService for writing to the Application event log.

Because the StartType of the OrderService Windows service is set as Automatic, it will start automatically the next time Windows starts.

Starting and Testing a Windows Service

To see the OrderService Windows service in action, you need to first create the directory c:\orders because OrderService depends on it. In most cases, it is helpful to create any such required files or directories at the time of installation itself. However, for that you have to install the Windows service using a Setup package based on the Windows Installer. You'll learn how to do that later in Chapter 10.

Step By Step 6.4 shows how to use the OrderService service to automatically add new records in the Orders table of Northwind database whenever an XML file is created in the c:\orders directory.

STEP BY STEP

6.4 Starting a Windows Service

  1. Create a directory c:\orders . This is the directory where the incoming orders will be created as an XML file.

  2. Restart the computer. The OrderService service is started automatically when Windows is started.

  3. Right-click on the My Computer icon and select Manage from its shortcut menu. In the Computer Management window, navigate to System Tools, Event Viewer, Application. You see that OrderService has written an informational message to the Application event log. Double-click to open the message; you'll see that the message is about the successful start of OrderService as shown in Figure 6.7.

    Figure 6.7. When the StartType for a Windows service is set to Automatic, the service is started automatically when Windows is started.

  4. Create an XML file named Orders.xml (in some directory other than c:\orders ) and store the following information to this file:

     <?xml version="1.0" standalone="yes"?> <dsOrders xmlns="http://www.tempuri.org/dsOrders.xsd">   <Orders>     <CustomerID>VINET</CustomerID>     <EmployeeID>5</EmployeeID>     <OrderDate>2002-10-11</OrderDate>     <RequiredDate>2002-10-21</RequiredDate>     <ShippedDate>2002-10-18</ShippedDate>     <ShipVia>3</ShipVia>     <Freight>22.38</Freight>     <ShipName>Vins et alcools Chevalier</ShipName>     <ShipAddress>59 rue de l'Abbaye</ShipAddress>     <ShipCity>Reims</ShipCity>     <ShipRegion>MI</ShipRegion>     <ShipPostalCode>51100</ShipPostalCode>     <ShipCountry>France</ShipCountry>   </Orders> </dsOrders> 
  5. Copy this file to the c:\orders directory. You should see that in a few moments the Orders.xml file is moved from c:\orders to a new subdirectory c:\orders\Updated .

  6. Query the Orders table in the Northwind database. You'll find that a record has been added to the Orders table with the information from the Orders.xml file.

In Step By Step 6.4, you see that as the new XML files are created, they are inserted to the Orders table of the Northwind database. After the order record is inserted, the program also moved the XML file to the c:\orders\updated subdirectory. The FileSystemWatcher is configured not to listen to any of the subdirectories inside c:\orders ; therefore, XML files created in c:\orders\updated do not raise any events of interest to the FileSystemWatcher component.

REVIEW BREAK

  • You can easily create Windows services by using the functionality provided by the System.ServiceProcess.ServiceBase class.

  • The System.ServiceProcess.ServiceProcessInstaller and the System.ServiceProcess.ServiceInstaller classes provide the functionality for custom installation of Windows service applications.

  • In order for installation tools such as installutil.exe to install an assembly, the assembly must have a class derived from System.Configuration.Install.Installer . If any custom installers (such as Windows service installers) need to be installed, they are added to the Installers collection of the Installer class.

  • When the SCM sends a start message to a Windows service, the Main() method of the Windows service application is executed first. This method creates one or more instances of the Windows services and passes them to the Run() method of the ServiceBase class. This method provides the references of Windows service objects to the SCM. SCM uses these references to communicate with the Windows service.

GUIDED PRACTICE EXERCISE 6.1

Recall from Chapter 3, ".NET Remoting," that when you want to remote an object other than using IIS, you have to create a remoting server and manually start the service whenever the computer is restarted. When you implement a remoting server as a Windows service, it is easy to ensure that the remoting server is automatically started with Windows.

The objective of this exercise is to create a Windows service, which exposes the DbConnect class of the StepByStep3_10 project as a Singleton server activated object. You'll use a client program such as the one created in Step By Step 3.12 to connect to this service.

The server and client should communicate via the TCP channels and the binary formatter. The service should start automatically when Windows is started. The service should allow itself to be stopped but should not support pause and continue operations.

How would you create such a Windows service?

You should try working through this problem on your own first. If you get stuck, or if you'd like to see one possible solution, follow these steps:

  1. Add a new Visual Basic .NET Windows Service application named GuidedPracticeExercise6-1 to the solution.

  2. Add references to the .NET assembly System.Runtime.Remoting and the StepByStep3-9 and StepByStep3-10 projects that you created in Chapter 3 (the interface to the remotable class and the remotable class itself).

  3. In the Solution Explorer, rename the default Service1.vb to DbConnect.vb . Open the file and change the name of the class to DbConnect in the class declaration. Click on the designer surface; then in the Properties windows, set the Name and ServiceName properties to DbConnect . Note the CanPauseAndContinue property is already set to False .

  4. Switch to the code view and change all occurrences of Service1 to DbConnect . Add the following statements to the code:

     Imports StepByStep3_9 Imports StepByStep3_10 Imports System.Runtime.Remoting Imports System.Runtime.Remoting.Channels Imports System.Runtime.Remoting.Channels.Tcp 
  5. In the code, search for the skeleton of the OnStart() method and modify it as shown here:

     Protected Overrides Sub OnStart(_     ByVal args() As String)     ' Register a TCP server channel that     ' listens on port 1234     Dim channel As TcpServerChannel = _      New TcpServerChannel(1234)     ChannelServices.RegisterChannel(channel)     ' Register the service that publishes     ' DbConnect for remote access in Singleton mode     RemotingConfiguration. _      RegisterWellKnownServiceType(_      GetType(StepByStep3_10.DbConnect), "DbConnect", _      WellKnownObjectMode.Singleton) End Sub 
  6. Access the Properties window for the DbConnect component. Click on the Add Installer hyperlink. This adds a component named ProjectInstaller.vb to the project. Access the design view for ProjectInstaller.vb . You'll see that it has two components ServiceProcessInstaller1 and ServiceInstaller1 .

  7. Access the properties for the ServiceProcessInstaller1 component. You'll note that this component is an instance of the ServiceProcessInstaller class. Change its Account property from User to LocalSystem .

  8. Access the properties for the ServiceInstaller1 component; you'll note that this component is an instance of the ServiceInstaller class. Change its ServiceName and DisplayName properties to DbConnect and the StartType property to Automatic .

  9. Set DbConnect as the startup object for the project.

  10. Build the project. This step creates a remoting server that is capable of registering the StepByStep3_10.DbConnect class for remote invocation using the Singleton activation mode via the TCP channel.

  11. To install the Windows service, open the Visual Studio .NET Command Prompt and change the directory to the bin\debug directory in the project folder. Execute the following command:

     installutil GuidedPracticeExercise6-1.exe 
  12. Use the command prompt to execute the following command in order to start the Windows service:

     NET START DbConnect 
  13. Run the executable file StepByStep3-12.exe for the project created in Step By Step 3.12. In the window that appears, type queries for retrieving data from the Northwind database.

If you have difficulty following this exercise, review the sections "Creating a Windows Service Application" and "Installing a Windows Service" earlier in this chapter. Also review the sections, "Creating a Remotable class" and "Creating a Server-Activated Object" in Chapter 3. Make sure that you also perform Step By Step 6.1 through Step By Step 6.3 and Step By Step 3.9 through Step By Step 3.12. After doing that review, try this exercise again.


   
Top


MCAD. MCSD Training Guide (Exam 70-310. Developing XML Web Services and Server Components with Visual Basic. NET and the. NET Framework)
MCAD/MCSD Training Guide (70-310): Developing XML Web Services and Server Components with Visual Basic(R) .NET and the .NET Framework
ISBN: 0789728206
EAN: 2147483647
Year: 2002
Pages: 166

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