Deploying a Windows Service



Deploying a Windows Service

Create a setup program that installs a Windows service, a serviced component, a .NET Remoting object, and an XML Web service.

  • Register components and assemblies.

Configure client computers and servers to use a Windows service, a serviced component, a .NET Remoting object, and an XML Web service.

Unlike a Web service, a Windows service application is installed in a directory on the file system of the target computer. You therefore use a Setup Project template rather than a Web Setup Project template to deploy a Windows service.

In addition, you also need to register a Windows service with the Windows Service Control Manager. The .NET Framework provides custom installation components to take care of this. You can install these components by configuring them as a custom action in the Custom Actions Editor of the setup projects.

I already discussed how to install a Windows service in Chapter 6. For background, I recommend you complete Step-by-Step 6.1 and Step-by-Step 6.2, if you have not already done so. In this section, I'll use the OrderService Windows service from Chapter 6 and discuss how to package it in a Windows installer package.

STEP BY STEP

10.17 Creating a Setup Project for the OrderService Windows Service

  1. Add an existing Windows service project StepByStep6_1 (OrderService, created in Chapter 6) to the solution.

  2. Launch WordPad and create and add a file named License.rtf to the StepByStep6_1 project folder with the following text:

    
    OrderService End User License Agreement
    
    You should carefully read the following terms and
    
    conditions before using this software.
    
    If you do not agree to any of the terms of this
    
    License, then do not install, distribute, or use this
    
    copy of OrderService. This software, and all
    
    accompanying files, data, and materials, are
    
    distributed AS IS and with no warranties
    
    of any kind, whether express or implied. Good data
    
    processing procedure dictates that any program be
    
    thoroughly tested with non-critical data before
    
    relying on it. The user must assume the entire risk
    
    of using the program.
    
    (c) All rights reserved.
    
    
  3. Add a new project to the solution. In the Add Project dialog box, select Setup and Deployment projects from the Project Types tree and select Setup Project from the list of templates on the right. Name the project OrderServiceSetup.

  4. In Solution Explorer, right-click the project and select Add, Project Output from the context menu. In the Add Project Output Group dialog box, select StepByStep6_1 as the project and select Primary Output from the list box. Click OK.

  5. In Solution Explorer, right-click the project and select Add, File from the context menu. In the Add Files dialog box, select the License.rtf added to the StepByStep6_1 project in step 2. Click Open.

  6. Select the OrderServiceSetup project in Solution Explorer. Open the User Interface Editor by clicking the User Interface Editor icon in Solution Explorer.

  7. Right-click the Start node under the Install tree and select Add Dialog from the context menu. The Add Dialog dialog box appears. Select License Agreement, as shown in Figure. Click OK. Right-click License Agreement and choose Move Up twice to move the License Agreement dialog box to appear after the Welcome dialog box as shown in Figure. Select the LicenseFile property in the Properties window and select (Browse...) from the drop-down list. The Select Item in Project dialog box appears. Navigate to the Application Folder and select License.rtf.

    29. You can use the Add Dialog dialog box to add dialog boxes in the User Interface Editor.

    graphics/10fig29.jpg

    30. You can customize the user interface of the installation program via the User Interface Editor.

    graphics/10fig30.jpg

  8. Open the Custom Actions Editor by clicking the Custom Actions Editor icon in Solution Explorer.

  9. Right-click the Custom Actions node in the editor and select Add Custom Action from the context menu. In the Select Item in the project dialog box, select Primary output from StepByStep6_1 (Active) under the Application Folder. Click OK.

  10. Select the new project in Solution Explorer. Activate the Properties window. Set Manufacturer to OrderService Corp, set ProductName to OrderService, and Title to OrderService Windows Service.

  11. Build the OrderServiceSetup project. Install the project. A License Agreement screen appears. Only when you select the I Agree option is the Next button enabled, as shown in Figure.

    31. The customized License Agreement dialog box is added to the user interface to display the license agreement during the installation process.

    graphics/10fig31.jpg

    NOTE

    The LicenseFile Property The LicenseFile property of the License Agreement dialog box works only with Rich Text Format (RTF) files. If you specify a file of any other format, you don't get any error, but the contents of the file are not displayed in the license agreement.

  12. Click Next and you should see the Select Installation Folder screen, as shown in Figure. Change the installation folder if you want to and click Next and then click Close.

    32. The Select Installation Folder screen enables the user to specify a custom installation folder during the installation process.

    graphics/10fig32.jpg

  13. Open the Services administrative tool from the Administrative tools section of the Windows Control Panel. You should see the OrderService being added to the Services on the target machine.

In Step-by-Step 10.17, you learned how to use the Custom Actions Editor to install the predefined installation component. In addition, you also learned how to use the User Interface Editor to display custom screens, such as a License Agreement screen, during the setup process.

REVIEW BREAK

  • Installation components enable you to take custom actions during the setup of an application. The .NET Framework provides a few predefined installer components for use with server components. You can also create custom installation components by deriving from the Installer class.

  • The Windows Installer Service and the Installer tool (installutil.exe) perform installation in a transactional manner.

  • If you want a custom installer class to execute when an assembly is installed—whether you use the setup projects or the Installer tool—then you need to apply the RunInstaller attribute to the class and set its value to true.

  • You use the Setup Project template to create a setup program for a Windows Service.