Deploying a Serviced Component



Deploying a Serviced Component

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.

The two main steps to deploy a serviced component are as follows:

  1. Register the serviced component with the COM+ catalog— There are three different ways for registration:

    • Lazy Registration— The .NET Framework automatically registers a serviced component with the COM+ catalog when the component is requested for the first time. The registration of a component requires administrative privileges and the component registration will fail if that application's first user does not have administrative privileges. This option may be used while developing or testing a component but is not a good idea for deployment.

    • Manual Registration— You can use the Component Services administrative tool (regsvcs.exe) to manually register a serviced component with the COM+ catalog. This technique of registration is useful in combination with XCOPY deployment of the serviced component.

    • Programmatic Registration— You can use the RegistrationHelper class to programmatically register a serviced component with the COM+ catalog. This registration technique is what you will use when creating a Windows Installer–based setup package for the deployment of a serviced component.

  2. Install the serviced component assembly at a location where the CLR can locate it— Where the CLR locates an assembly also depends on the program that makes the request to an assembly. Therefore, the installation location may depend on how the component is registered with the COM+ catalog. There are two ways to register a serviced component in the COM+ catalog:

    • Library Application— The serviced component, which is installed as a library application, is created in the process of the calling application. In this case, the serviced component may be deployed as a private application within the calling application directory structure. But if the serviced component is used by multiple calling applications, rather than introduce redundancy by copying the serviced component in the directory of each calling application, it is a better idea to install the serviced component in the GAC.

    • Server Application— The serviced component, which is installed as a server application, is created in the process of dllhost.exe, which is located in the Windows System directory. Obviously, a better installation choice in this case also is the GAC.

From the preceding list, it is clear that in most cases you would install a serviced component assembly in the GAC. You would also either use the Component Services administrative tool for registration of a serviced component in the COM+ catalog, or you would use the RegistrationHelper class when programmatically registering a serviced component—for example, when registering through a setup program. I already covered the basics of COM+ catalog registration and discussed the use of the Component Services administrative tool in Chapter 7, "Component Services."

In this chapter, I'll cover the following two techniques to deploy a serviced component:

  • Using a Visual Studio .NET setup and deployment project

  • Using the Component Services administrative tool

Using a Visual Studio .NET Setup and Deployment Project to Deploy a Serviced Component

Step-by-Step 10.18 shows how to deploy a serviced component. In this exercise, you use a Visual Studio .NET setup project to deploy a serviced component. If the serviced component is deployed as a part of a Web application, you can also use a Web setup project for its deployment.

STEP BY STEP

10.18 Creating a Setup Project for the NorthwindSC Serviced Component

  1. Add an existing Windows service project StepByStep7_8 (NorthwindSC, created in Chapter 7) to the solution.

  2. Right-click the project StepByStep7_8 and choose Add, Add New Item from the context menu. The Add New Item dialog box appears. Select Installer Class from the right pane. Name the new class NorthwindSCInstaller.cs.

  3. Add a reference to the System.Windows.Forms.dll to the StepByStep7_8 project by using the Add Reference dialog box.

  4. Open the NorthwindSCInstaller.cs file in the code view and add the following using directive:

    
    using System.EnterpriseServices;
    
    using System.Windows.Forms;
    
    
  5. Add the following code after the Component Designer–generated code section in the class definition:

    
    public override void Install(
    
        System.Collections.IDictionary stateSaver)
    
    {
    
        try
    
        {
    
            string application = null;
    
            string tlbimp = null;
    
    
    
            // Get the location of the assembly
    
            string scassembly =
    
                GetType().Assembly.Location;
    
    
    
            // Install the assembly in the COM+ catalog
    
            RegistrationHelper rh =
    
                new RegistrationHelper();
    
            rh.InstallAssembly(scassembly,
    
                ref application, ref tlbimp,
    
                InstallationFlags.
    
                FindOrCreateTargetApplication);
    
    
    
            // Save the application name and assembly
    
            // location in the InstallState file
    
            stateSaver["application"] = application;
    
            stateSaver["scassembly"] = scassembly;
    
        }
    
        catch(Exception ex)
    
        {
    
            MessageBox.Show(
    
                ex.Message, "Install Exception");
    
        }
    
    }
    
    
    
    public override void Uninstall(
    
        System.Collections.IDictionary savedState)
    
    {
    
        try
    
        {
    
            // Retrieve the application name and assembly
    
            // location from the InstallState file
    
            string application =
    
                (string) savedState["application"];
    
            string scassembly =
    
                (string) savedState["scassembly"];
    
    
    
            // Uninstall the assembly from the COM+ catalog
    
            RegistrationHelper rh =
    
                new RegistrationHelper();
    
            rh.UninstallAssembly(scassembly, application);
    
        }
    
        catch(Exception ex)
    
        {
    
            MessageBox.Show(
    
                ex.Message, "Uninstall Exception");
    
        }
    
    }
    
    
  6. Build the StepByStep7_8 project.

  7. 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 NorthwindSCSetup.

  8. 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 StepByStep7_8 as the project and select Primary Output from the list box. Click OK.

  9. Open the Custom Actions Editor for the NorthwindSCSetup project. Right-click the Install node under the Custom Actions node and then select Add Custom Action from the shortcut menu. The Select Item in Project dialog box appears. Look in the Application Folder and select Primary Output from StepByStep7_8 (Active). Click OK. The primary output is added to the Install node.

  10. Now select the Uninstall node and select Add Custom Action from the context menu. The Select Item in Project dialog box appears. Look in the Application Folder and select Primary Output from StepByStep7_8 (Active). Click OK. The primary output is now also added to the Uninstall node, as shown in Figure.

    33. You can perform custom actions by using the Custom Actions Editor.

    graphics/10fig33.jpg

  11. Open the File System Editor for the NorthwindSCSetup project. Select the File System on Target Machine node and choose Add Special Folder, Global Assembly Cache Folder from the context menu. Select the Global Assembly Cache Folder and select Add, Project Output from the context menu.

  12. The Add Project Output Group dialog box appears. Select the StepByStep7_8 project and select Primary Output from the list of items to be added. Click OK. The File System Editor now appears as shown in Figure.

    34. You can add files to special folders on the target machine by using the File System Editor.

    graphics/10fig34.jpg

  13. Open the Registry Editor for the NorthwindSCSetup project. Select the HKEY_LOCAL_MACHINE node and hierarchically add new keys to the node in the order HKEY_LOCAL_MACHINE, Software, Microsoft, .NETFramework, AssemblyFolders, and NorthwindSCCorp, as shown in Figure.

    35. You can set the keys and values in the Registry with the help of the Registry Editor.

    graphics/10fig35.jpg

  14. Add a new string value to the newly added NorthwindSCCorp key. Select the value and invoke the Properties window. Empty the Name property. When you do this, the (Default) name of the value is set (refer to Figure). Set the Value property to [TARGETDIR]. This is the folder where the assembly file copy is stored, along with the GAC.

  15. Select the new project in Solution Explorer. Activate the Properties window. Set Manufacturer to NorthwindSC Corp, set ProductName to NorthwindSC Serviced Component, and set Title to NorthwindSC Serviced Component Installer.

    WARNING

    Working with the Registry Be extra careful when working with the Windows Registry. Take special care with the DeleteAtUninstall property of the Registry Settings Properties. Setting DeleteAtUninstall to true for a wrong key (such as HKEY_LOCAL_MACHINE\SOFTWARE) might have a very bad impact on the target computer.

  16. Build the NorthwindSCSetup project. Install the project.

  17. Open the Component Services administrative tool from the Administrative Tools section of the Windows Control Panel. You should see the NorthwindSC component in the Northwind Data Application with Object Pooling application is being added to the Component Services on the target machine.

In Step-by-Step 10.18, you used a custom installer component to register the serviced component with the help of the RegistrationHelper class. The Installer component is stored in the assembly of the serviced component itself. In the Custom Actions Editor, you add the output of this project to the Install and the Uninstall node. This instructs the setup program to activate the Install() and Uninstall() methods from the assembly at the time of deployment.

In Step-by-Step 10.18, you also learned how to place assemblies in the GAC. You learned how to make entries to the Windows Registry. You added a Registry, which enables the serviced component assembly to be automatically displayed in the Visual Studio .NET Add Reference dialog box.

Using the Component Services Administrative Tool to Deploy a Serviced Component

The Component Services administrative tool provides a wizard to export a configured application as a Windows Installer package. This tool also provides a wizard to create a new COM+ application based on the information stored in a Windows Installer package. This deployment technique is most useful for transferring already configured COM+ applications from one computer to another. You can export the configured application as a Windows Installer package from one computer. Transfer the package to another computer and then use the COM+ Install wizard to create a new COM+ application based on it.

However, this COM+ Install wizard is of little use in installing new serviced components from a Windows Installer package that is created by Visual Studio .NET setup projects because COM+ can't recognize that.

Step-by-Step 10.19 shows how to export a COM+ application as a Windows Installer package.

STEP BY STEP

10.19 Using the Component Services Tool to Create an Installation Package for the NorthwindSC Serviced Component

  1. Open the Component Services administrative tool from the Administrative Tools section of the Windows Control Panel.

  2. Right-click the Northwind Data Application with Object Pooling (created in StepByStep7_8) in the Component Services administrative tool and select Export from the context menu.

  3. The welcome screen of the COM Application Export Wizard appears. Click Next.

  4. The second screen of the wizard is the Application Export Wizard screen. Enter the full path and filename where the package file needs to be exported, as shown in Figure. Also, in the Export as Group box, select the Server Application option (see Figure). Click Next and then click Finish.

    36. The Application Export Wizard screen of the wizard enables you to specify information required to export the COM+ application.

    graphics/10fig36.jpg

  5. Browse to the destination folder, where the package files are exported. You should see two files, NorthwindSC.msi and NorthwindSC.msi.cab, being created.

Now you have all the settings for the COM+ application, as well as the DLL files for the serviced component, in the MSI file. Step-by-Step 10.20 shows how to use the Component Services administrative tool to create a new COM+ application based on the information in the MSI file.

STEP BY STEP

10.20 Installing an Installation Package for the NorthwindSC Serviced Component Created with the COM+ Application Export Wizard

  1. Open the Component Services administrative tool from the Administrative Tools section of the Windows Control Panel.

  2. Right-click the COM+ Application node and select New, Application from the context menu.

  3. The welcome screen of the COM Application Install Wizard appears. Click Next.

  4. The second screen of the wizard is the Install or Create a New Application screen. Click the Install Pre-built Application(s) button, as shown in Figure.

    37. The Install or Create a New Application screen of the wizard enables you to install a pre-built application or create a new application.

    graphics/10fig37.jpg

  5. This opens the Install from Application File dialog box. Browse and select the NorthwindSC.msi package created in Step-by-Step 10.19. The Select Application Files screen of the wizard with the application file appears as shown in Figure. Click Next.

    38. The Select Application Files screen of the wizard enables you to choose the files for the application you need to install.

    graphics/10fig38.jpg

  6. The Set Application Identity screen of the wizard appears. Select the Interactive user option, as shown in Figure. You can also provide user and password details if you want to install the application with a different identity. Click Next.

    39. The Set Application Identity screen of the wizard enables you to specify the application identity.

    graphics/10fig39.jpg

  7. The Application Installation Options screen of the wizard appears. Select the Specific Directory option and specify a desired install directory for the component files, as shown in Figure. Click Next and then click Finish.

    40. The Application Installation Options screen of the wizard enables you to specify the install directory and the role configuration.

    graphics/10fig40.jpg

    You should see the COM+ application Northwind Data Application with Object Pooling being added to the Component Services on the target machine.

  8. Browse to the install directory selected in step 7. You should see that two component files, StepByStep7_8.dll and StepByStep7_8.tlb, have been created.

Because the Component Services administrative tool was created before .NET, it does not allow you to install the assemblies in the GAC. If you want to use the COM+ Install wizard to install .NET applications, you must also take an extra step to register the serviced component in the GAC.