Deploying a Web Service



Deploying a Web Service

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

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

You can deploy a Web service on a Web server in a variety of ways:

  • By manually copying the files

  • By using the Copy Project command of Visual Studio .NET

  • By using the Windows Installer package created with the Visual Studio .NET Web setup project

I'll present each of these techniques in the following subsections.

Deploying a Web Service by Manually Copying the Files

Deploying a Web service by manually copying the files is a four-step process:

  1. Build the Web service— When you build a Web service application, all the code-behind files of the Web services are compiled into a DLL file and are stored in the bin subdirectory of the Web service application. This step enables you to deploy the compiled DLL files rather than the source files.

  2. Select the files that need to be deployed— A Web service may need to deploy several additional files in addition to the DLL files created in step 1. Figure lists the various files that are created in a Visual C# .NET Web Service project and whether the file needs to be deployed. In addition to these files, you might also use some other files such as an .xml file or an .xsd file, which you may require on the production Web server.

    Files in a Visual C# .NET Web Service Project

    Web Service File

    Description

    Needs to be Deployed?

    .asmx

    This file contains the WebService processing directive and serves as the entry point for the XML Web service.

    Yes

    .asmx.cs

    This file is a code-behind file that contains the source code for the XML Web Service. When you build the Web service, content of this file is compiled into a DLL file.

    No

    AssemblyInfo.cs

    This file contains metadata about the assemblies in a project, such as name, version, and culture information. The contents of this file are also compiled into a DLL file.

    No

    web.config

    An XML-based file that contains configuration information for the Web service.

    Yes

    global.asax

    A file that contains information for handling application-level events.

    Yes

    global.asax.cs

    The code-behind file that contains the source code for the global.asax. When the Web service is built, the contents of this file are compiled into a DLL file.

    No

    .csproj

    This file contains settings for a Web service project for use by Visual C# .NET. This file is used only at the time of development.

    No

    .csproj.webinfo

    Contains the path to the project on the development server.

    No

    .vsdisco

    A file that provides dynamic discovery for all the Web services on a server. You should not usually deploy this file to the production server because in doing so, you will lose control over which Web services you want to make available to others.

    No

    .disco

    A file that provides static discovery of Web services. This file includes only the Web services that you explicitly want to expose to others.

    Yes

    .dll

    The assembly that contains the compiled output for the Web service project.

    Yes

    .pdb

    A file that contains the debug symbols for the XML Web service and provides support for debugging of the XML Web service on the server. This file is created only when the project is compiled in the debug mode.

    No

    .sln

    The file that stores the metadata of the Web service solution.

    No

  3. Copy the Files to the production server— After you have identified the files that are required for deployment, the next step is to deploy all these files to a folder on the production server. You can use any copy mechanism, such as XCOPY or FTP, to accomplish this.

  4. Configure the Web service folder on the production server— Just copying the files to the production server is not enough; you also need to configure the target folder as a virtual directory, set access permissions for the virtual directory, and configure the virtual directory as an IIS application.

In addition to these steps, you may also need to perform additional steps, such as installing an assembly to the global assembly cache (GAC), or registering a serviced component with the COM+ catalog, setting up data access to SQL Server, and so on.

Deploying a Web Service by Using the Visual Studio .NET Copy Project Command

For Web applications, Visual Studio .NET provides a Copy Project command, which can help developers to quickly copy all the files in a project to a different location in the same machine or a different machine.

To use this tool, take the following steps:

  1. Open the Web application that needs to be deployed in Visual Studio .NET.

  2. Select Project, Copy Project. You see the dialog box as shown in Figure

    16. The Copy Project dialog box enables you to copy a Web project to the same server or to a different server.

    graphics/10fig16.jpg

  3. Select the Web access method as FrontPage if you need the target project to be automatically set up as an IIS application.

  4. Select the Copy Files Needed to Run This Application option if you want to deploy only the files necessary to run the Web application.

Because the Copy Project command can also set the destination directory as an IIS application, it saves an extra step that would ordinarily be required when you use XCOPY or FTP to manually copy the files. As a Visual Studio .NET developer, this command is an easy way to transfer your application quickly to a different computer and test the results.

However, this technique may not be sufficient to deploy a Web service on the production Web server because the Copy Project command does not provide any options for configuring the settings of the virtual directory, such as an option for configuring directory security. In addition, the Copy Project command still shares many of the same limitations of the XCOPY command that were mentioned in the previous section.

Deploying a Web Service by Using a Windows Installer Package

Another option for deploying a Web service is by using the Visual Studio .NET Web setup project. You can use the various editors available in the Web setup project to automate most of the steps required for deploying a Web service to the target computer.

In Step-by-Step 10.16, you use the Web setup project to create a Windows Installer setup package for deploying the Northwind Web service that you created in Chapter 4, "Basic Web Services." You use the File System Editor to copy all the required files and use the Launch Conditions Editor to ensure that the Web service is installed on the target computer only if the target computer has support for Microsoft Data Access Components (MDAC) 2.7.

STEP BY STEP

10.16 Creating a Web Setup Project for the Northwind Web Service

  1. Right-click the solution and select Add, Add Existing Project in Solution Explorer. Browse to the C:\Inetpub\Northwind directory and add the Northwind Web service project (created in Chapter 4) to the solution.

  2. Add a new project to the solution. Select Setup and Deployment Projects from the Project Types tree and then select Web Setup Project from the list of templates on the right, as shown in Figure.

    17. The Web Setup Project can be used to deploy a Web service application.

    graphics/10fig17.jpg

  3. In Solution Explorer, right-click the project and select Add, Project Output from the context menu. This opens the Add Project Output Group dialog box, as shown in Figure. Select Northwind as the Project and select Primary Output and Content Files from the list box. Click OK.

    18. The Add Project Output Group dialog box enables you to add Web project resources to the Web Setup project.

    graphics/10fig18.jpg

  4. Select the project in Solution Explorer. Activate the Properties window. Set the Manufacturer to Northwind Corp and the ProductName to Northwind Web Service and Title to Northwind Web Service Installer, as shown in Figure.

    19. The Properties window enables you to set properties of the Web Setup project.

    graphics/10fig19.jpg

  5. Open the File System Editor for the NorthwindSetup project by clicking the File System Editor icon in Solution Explorer. Select the Web Application Folder under the File System on the Target Machine node in the left pane of the editor.

  6. Invoke the Properties window. Change the VirtualDirectory property to NorthwindApp, as shown in Figure.

    20. You can configure some of the IIS settings through the properties of the Web Application Folder of the Web setup project.

    graphics/10fig20.jpg

  7. Open the Launch Conditions Editor for the NorthwindSetup project by clicking the Launch Conditions Editor icon in Solution Explorer.

  8. Select the Requirements on Target Machine node and select Action, Add Registry Launch Condition. This adds two nodes, one under the Search Target Machine node and another under the Launch Conditions node.

  9. Select the newly created node under the Search Target Machine node and open the Properties window. Set the Name property to Search for MDAC Support, the Property property to MDACSUPPORT, the RegKey property to Software\Microsoft\DataAccess, the Root property to vsdrrHKLM, and the Value property to FullInstallVer, as shown in Figure.

    21. You can perform a search on Registry on the target Web server during the installation process via the Launch Conditions Editor.

    graphics/10fig21.jpg

  10. Select the newly created node under the Launch Conditions Node and open the Properties window. Set the Name property to MDACSupport, the Condition property to MDACSUPPORT >= "2.7" and the Message property to You must have MDAC version 2.7 or higher installed on this computer. Please contact the administrator for installation information., as shown in Figure.

    22. You can add launch conditions on the target Web server during the installation process via the Launch Conditions Editor.

    graphics/10fig22.jpg

  11. Build the NorthwindSetup project. Open Windows Explorer and navigate to the Release folder inside the project folder. Run setup.exe. Alternatively, on the development machine, you can install by right-clicking the project in Solution Explorer and choosing the Install option from the context menu. This opens the Northwind Web Service Setup Wizard with a welcome screen. Click Next. The Select Installation Address screen appears, as shown in Figure. Select the default settings and click Next. Click Next again to start the installation. Click Close. Note that if the target computer does not have MDAC 2.7 or higher installed, the installation process of the NorthwindSetup project fails and you get the error message set in the Message property of the launch condition.

    23. The Select Installation Address screen prompts the user to specify the virtual directory during the installation process.

    graphics/10fig23.jpg

    NOTE

    Configuring IIS Settings In the case of Web applications and Web services, you may need to configure IIS settings as part of the deployment process. You can use the properties of the Web Application Folder in the Web setup project to accomplish this, as shown in Figure.

  12. Open Internet Explorer and navigate to http://localhost/NorthwindApp/Customer.asmx. (If the Web server is different from the local machine, substitute the server name for localhost.) Verify that the GetCustomers() Web method works as expected.

  13. Select Settings, Control Panel, Add/Remove Programs from the Windows start menu to open the Add/Remove Programs dialog box. Select the Change or Remove Programs Icon from the left pane and select the Northwind Web Service application from the right pane. Click the Remove button to uninstall the Northwind Web Service application and click the Yes button. The Northwind Web Service application will be uninstalled from your application. Alternatively, on the development machine, you can also uninstall by right-clicking the setup project in Solution Explorer and choosing the Uninstall option from its context menu.

WARNING

Choosing a Virtual Directory If you are testing your project and deploying the Web application on the same machine, make sure that you don't choose an existing virtual directory because you might lose the existing files in that directory.


The preceding exercise illustrated the process of creating a Web setup project. When the project is compiled, the output files are placed in the Release or Debug folder, depending on the active configuration. The contents of the folder are an installer package (.msi), executables (.exe) and initialization (.ini) files. The .msi file is the installation package in the Microsoft Windows Installer format. If the Windows Installer Service is installed on your computer, you can directly start the installation by double-clicking this file. The executable files consist of Setup.exe, InstMsiA.exe, and InstMsiW.exe. Setup.exe (also called the Windows Installer Bootstrapper) bootstraps the installation process by first testing for the presence of the Windows Installer service on the target machine. If the Windows Installer service is not installed, the bootstrapper will first install it using either InstMsiA.exe (for Windows 9x and Me) or InstMsiW.exe (for Windows NT/2000/XP/.NET), and then instruct the Windows Installer service to execute the installation based on the information stored in the installation package (.msi file). The setup.ini file stores the initialization settings, such as name of the installation database, for the bootstrap file setup.exe.

But there is one catch here. This setup project works only on computers where the .NET Framework runtime has been installed already. In fact, when you build the setup project, you will see the following message in the Output window of Visual Studio .NET:

WARNING: This setup does not contain the .NET Framework which must be installed on the target machine by running dotnetfx.exe before this setup will install. You can find dotnetfx.exe on the Visual Studio .NET 'Windows Components Update' media. Dotnetfx.exe can be redistributed with your setup.

On the other hand, when you look at the Web setup project folder in the Solution Explorer window, you will see that it had created a Detected Dependencies folder and included a file named dotnetfxredist_x86_enu.msm in it. But the Exclude property of this file is set to true. This is just a placeholder module that stops Visual Studio .NET from automatically including the .NET Framework files from your installation of the .NET Framework into the project. If you try changing the Exclude property of the dotnetfxredist_x86_enu.msm file to false, Visual Studio .NET won't allow you and you will get an error at the time of building the project. The error message would say "ERROR: dotNETFXRedist_x86_enu.msm must not be used to redistribute the .NET Framework. Please exclude this merge module." Ideally, you should leave the Exclude property at its default value of true for the dotNETFXRedist_x86_enu.msm dependency.

Actually, the .NET Framework is a complex system, and installing the .NET Framework by just redistributing the files is not enough to get it to work successfully on the target machine. The merge module dotNETFXRedist_x86_enu.msm is included in the setup project as a proxy to prevent the dependency resolution system from detecting some of the .NET framework files and attempting to redistribute them as part of the setup package.

Ideally, the .NET Framework must be installed separately. There are several ways to accomplish this:

  • Ask the user to run setup for .NET Framework from the Windows Component Upgrade CD that comes with Visual Studio .NET.

  • Ask the user to download the .NET Framework from the Microsoft MSDN Download Center or from the Microsoft Windows Update Website, http://windowsupdate.microsoft.com.

  • Use the .NET Framework bootstrapper (Setup.exe) that checks for the availability of the .NET Framework and installs it if it is not already installed.

Of course, for a professional setup program, you would not like to leave it to the users to perform a manual installation of the .NET Framework. Using the .NET Framework bootstrapper Setup.exe is a good idea. Microsoft provides a sample bootstrapper Setup.exe that you can readily use in your projects. I will tell you where to get and how to use this bootstrapper Setup.exe later in this chapter in Exercise 1. For now, I will assume that the .NET Framework is available on the machine where you will deploy your applications.

You can modify the configuration settings for a Web setup project by selecting the project in Solution Explorer and choosing Project, Properties from the main menu. This opens up the Project Property Pages dialog box as shown in Figure.

24. The Property Pages dialog box displays the configuration properties of the Web setup project.

graphics/10fig24.jpg

There are five main configuration properties:

  • Output file name— Specifies the output filename of the installation package (.msi file).

    NOTE

    Use Application Center to Deploy Web Applications and Windows Services to a Web Farm or a Cluster Microsoft Application Center provides the synchronization capability to manage the deployment process across multiple servers in a Web farm or a clustered environment. For example, you can script Application Center to apply specific IIS settings on a Web server's virtual directory and the Application center will take care of applying that setting to all the Web servers involved. For more information on Application Center, see www.microsoft.com/applicationcenter.


  • Package files— Specifies how to package the files. The options are, As Loose Uncompressed Files, In Setup File, and In Cabinet File(s). If the In Cabinet File(s) option is selected, then you can also specify the maximum size of the cab files.

  • Bootstrapper— Specifies whether any bootstrap file needs to be created for launching the installation program. A bootstrap is required when the target machine does not have the Windows Installer service already installed. The None option generates only the installation package (.msi file). The Windows Installer Bootstrapper option creates a Setup.exe file capable of installing the Windows Installer service if that is not already installed.

  • Compression— Specifies whether to optimize the installation files for size or speed or whether no optimization is required. The value Optimized for Speed uses a faster compression algorithm that unpacks the files quickly at install time. A faster compression algorithm usually results in a larger installation file. The value Optimized for Size uses a compression algorithm that runs more slowly but packs the files tightly. If None is selected then no optimization is performed.

  • Authenticode signature— Enables you to specify the file containing the Authenticode certificate, Private key file, and the Timestamp server URL (provided by the certification authority).

The output files generated can be deployed (copied) to any target machine and then installed and later uninstalled. When an application is uninstalled, all the actions performed by the installer application during the installation on the target machine are undone, leaving the target machine to its original state. You can also choose to repair or re-install the application by clicking the Change button in the Add/Remove Programs dialog box.

Publishing Web Services

Before other developers can use a Web service, they must know about its location. You can provide a discovery mechanism on the Web server by deploying a discovery file (.disco) on your Web server or by registering the Web service in the UDDI Registry. For more information about how to publish Web service discovery information, refer to Chapter 4.

GUIDED PRACTICE EXERCISE 10.1

Visual Studio .NET provides a Setup Wizard template in the Setup and Deployment Project category. The Setup Wizard template simplifies the process of creating a setup package using a step-by-step approach.

In this exercise, you are required to create a Windows Installer–based setup package for the StringProc Web service that you created in Chapter 4.

How would you create a setup package using the setup wizard?

In this exercise, you will practice creating a setup package by using the setup wizard. 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. Right-click the solution and select Add, Add Existing Project in Solution Explorer. Browse to the C:\Inetpub\StringProc directory and add the StringProc Web service project (created in Chapter 4) to the solution.

  2. Add a new project to the solution. Select Setup and Deployment Projects from the Project Types tree and then select Setup Wizard from the list of templates on the right.

  3. Name the project StringProcSetup. Click OK. The Setup Wizard appears. The first screen of the wizard is the Welcome screen. Click Next.

  4. The second screen of the wizard is the Choose a Project Type screen. Choose Create a Setup for a Web Application from the first group of options, as shown in Figure. Click Next.

    25. The Choose a Project Type screen in the setup wizard prompts the user to select the project type.

    graphics/10fig25.jpg

  5. The third page of the wizard is the Choose Project Outputs to Include screen. Select Primary Output from StringProc and Content Files from StringProc, as shown in Figure. Click Next.

    26. The Choose Project Outputs to Include screen in the setup wizard prompts the user to select the project output that the installer will deploy.

    graphics/10fig26.jpg

  6. The fourth page of the wizard is the Choose Files to Include screen. Click Next.

  7. The final page of the wizard is the Project Summary screen, as shown in Figure. Click Finish to create the project.

    27. The Project Summary screen in the setup wizard shows the summary of the files to be included in the setup project.

    graphics/10fig27.jpg

  8. Select the new project StringProcSetup in Solution Explorer. Activate the Properties window. Set Manufacturer to StringProc Corp, ProductName to StringProc Web Service, and Title to StringProc Web Service Installer.

  9. Open the File System Editor for the StringProcSetup project by clicking the File System Editor icon in Solution Explorer. Select the Web Application Folder under the File System on Target Machine node in the left pane of the editor. Invoke the Properties window. Change the VirtualDirectory property to StringProcApp.

  10. Build the StringProcSetup project. Right-click the project in Solution Explorer and select the Install option. Install the project.

  11. Open Internet Explorer and navigate to http://localhost/StringProcApp/Strings.asmx. (If the Web server is different from the local machine, substitute the server name for localhost.) Verify that the Web Service works as expected.

If you have difficulty following this exercise, review the sections "Deploying a Web Service" and "Using Visual Studio .NET to Create a Windows Installer Package" earlier in this chapter. After doing that review, try this exercise again.

REVIEW BREAK

  • When you deploy a Web service by manually copying the files, you need to manually create the virtual directory and set the virtual directory as an IIS application. The Visual Studio .NET Copy Project command does this automatically for you. The Web setup project provides the maximum configuration options and total control over the installation process.

  • Visual Studio .NET setup projects do not package the .NET Framework files for redistribution. You should ensure that the .NET Framework is already installed on the target machine, or better yet, you should use a bootstrapper program that installs the .NET Framework, if necessary, prior to installing the .NET application.

  • If you want other developers to be able to locate and use your Web service, consider publishing your Web service to the UDDI registry.