Export (0) Print
Expand All
47 out of 48 rated this helpful - Rate this topic

Redirecting an Application to Target a Different XML Web Service During Installation

Visual Studio .NET 2003
 

Steve Hoag
Visual Studio Team
Microsoft Corporation

November 2001

Summary: This article demonstrates how to create in Visual Basic® .NET a Web application that can be redirected to target a different XML Web service by using the URL Behavior property, an Installer class, and a Web Setup project. (8 printed pages)

Contents

Introduction
Make the URL Behavior Dynamic
Add an Installer Class
Add a Setup Project
Add the Installer Class as a Custom Action
Add a User Interface Dialog Box
Build and Install
Conclusion

Introduction

When developing applications that target XML Web services, you will most often target XML Web services that are already installed on a Web server. Although this is the norm, there will also be cases where you want to target a different XML Web service when the production version of your application is deployed. For example, you may have targeted a test version of an XML Web service on your development server, but the finished application needs to target an installed version of the XML Web service on another server. You might also want to allow the person installing the application to choose a version of the XML Web service during installation — for example, one that is on an intranet or one that can be accessed from outside the firewall.

This article demonstrates how to create a Web application that can be redirected to target a different XML Web service. The process includes:

  1. Specifying the URL Behavior property to locate an XML Web service at run time.
  2. Adding an installation component that performs a custom action at the end of installation.
  3. Using a Setup project to create an installer with a custom user interface for your application.

The first step is to create a Web application that has a Web reference to an XML Web service. For more information on creating a Web application, see Walkthrough: Creating an XML Web Service Using Visual Basic or Visual C# in the Visual Studio® .NET documentation.

Make the URL Behavior Dynamic

In order to locate an XML Web service at run time, an application stores the URL for the XML Web service. The URL Behavior property of a Web reference for an XML Web service determines where the URL is stored. When the URL Behavior property is set to Static (the default), the URL is hard-coded as part of the Web reference. When the URL Behavior property is set to Dynamic, an entry specifying the URL is added to the appSettings section of the application's Web.config file.

To set the URL Behavior property to Dynamic

  1. In Solution Explorer, expand the Web References node and select the reference to the XML Web service.
  2. In the Properties window, select the URL Behavior property and change it to Dynamic.
  3. In Solution Explorer, select the Web.config file and double-click to open it.

    Near the end of the file you should find an <appSettings> section that contains a key that specifies the name of the XML Web service and a value that specifies the URL. For example, for an application named MyWebApp that accesses MyWebService on the local computer, it would look like:

    <appSettings><add key="MyWebApp.localhost.Service1"
    value=http://localhost/MyWebService/Service1.asmx/></appSettings>

The next step is to add an Installer class that will be used to modify the .config file during installation.

Add an Installer Class

Installer classes, also known as installation components, are .NET Framework classes that are invoked as custom actions during installation. In this case, you will add an Installer class and override its Install method, adding code to modify the .config file. For more information on Installer classes, see Introduction to Installation Components in the Visual Studio .NET documentation.

To add an Installer class

  1. On the Project menu, choose Add New Item.
  2. In the Add New Item dialog box, select Installer Class and change the Name to WebServiceInstaller.

    When you click Open, the class will be added to your project and the designer for the Installer class will open.

  3. Double-click the designer to open the Code Editor.
  4. Add the following code for the Install method at the bottom of the Installer class module (just above the End Class declaration):
    Public Overrides Sub Install(ByVal stateSaver As 
    System.Collections.IDictionary)
       ' Get the parameter passed across in the CustomActionData.
       Dim installlog As New System.IO.StreamWriter("Installation.log")
       installlog.AutoFlush = True
       Try
          Dim ProvidedName As String 
    =Me.Context.Parameters.Item("ServerName")
          Dim SvcName As String = Me.Context.Parameters.Item("ServiceName")
          
          installlog.WriteLine("Starting Edit of the config file")
    
          If ProvidedName = "" Or SvcName = "" Then
             Throw New InstallException("No arguments specified")
          End If
    
          ' Use reflection to find the location of the config file. 
          Dim Asm As System.Reflection.Assembly = 
              System.Reflection.Assembly.GetExecutingAssembly
          Dim strConfigLoc As String
          strConfigLoc = Asm.Location
    
          Dim strTemp As String
          strTemp = strConfigLoc
          strTemp = strTemp.Remove(strTemp.LastIndexOf("\"), 
              Len(strTemp) – strTemp.LastIndexOf("\"))
          strTemp = strTemp.Remove(strTemp.LastIndexOf("\"), 
              Len(strTemp) – strTemp.LastIndexOf("\"))
    
          Dim FileInfo As System.IO.FileInfo = 
             New System.IO.FileInfo(strTemp & "\web.config")
    
          installlog.WriteLine("File info: " & strTemp)
    
          If Not FileInfo.Exists Then
             Throw New InstallException("Missing config file")
          End If
    
          ' Load the config file into the XML DOM.
          Dim XmlDocument As New System.Xml.XmlDocument()
          XmlDocument.Load(FileInfo.FullName)
    
          ' Find the right node and change it to the new value.
          Dim Node As System.Xml.XmlNode
          Dim FoundIt As Boolean = False
          For Each Node In 
             XmlDocument.Item("configuration").Item("appSettings")
             ' Skip any comments.
             If Node.Name = "add" Then
                If Node.Attributes.GetNamedItem("key").Value = 
    "appname.servername.service" Then
                   ' Note that "Service1.asmx" should be replaced with
                   ' the actual name of the web service file.
                   Node.Attributes.GetNamedItem("value").Value = 
                      "http://" & ProvidedName & "/" & 
                      SvcName & "/Service1.asmx"
                   FoundIt = True
                End If
             End If
          Next Node
    
          If Not FoundIt Then
             Throw New InstallException("Config file did not 
                contain a ServerName section")
             End If
    
             ' Write out the new config file.
             XmlDocument.Save(FileInfo.FullName)
    
       Finally
          installlog.WriteLine("Ending edit of config file")
          installlog.Close()
       End Try
    
    End Sub
    

    The above code first creates an installation log file that will record the progress of the custom action. The System.Reflection namespace is used to locate the assembly that is being installed and to find the associated .config file. The XML document model is used to iterate through the .config file until the appSettings section is found. When the key appname.servername.service is found, its associated value is changed to include the parameters that were passed in, redirecting the application to use the new XML Web service.

  5. In Solution Explorer, select the Web.config file and double-click to open it.
  6. Copy the value of the key for your XML Web service in the appSettings section. The key takes the form appname.servername.service where appname is the name of your application, servername is the server where the XML Web service is located, and service is the name of the XML Web service.
  7. Open the Installer class module in the Code Editor and replace appname.servername.service with the value that you copied in the previous step.

The next step is to add a Setup project for your application.

Add a Setup Project

Setup projects are used to create an installer for your application. Based on Windows Installer technology, setup projects include features such as the ability to run custom actions during installation and to customize the installation user interface. For more information on Setup projects, see Deploying Applications and Components in the Visual Studio .NET documentation.

To add a Setup project

  1. On the File menu, point to Add Project, then click New Project.
  2. In the Add New Project dialog box, select the Setup and Deployment Projects node in the Project Types pane.
  3. In the Templates pane, choose Web Setup Project and click OK.

    The project will be added to the solution and the File System Editor will be opened.

  4. In the Properties window, select the ProductName property and set it to the name of your application.
  5. In the File System Editor, select the Web Application Folder.
  6. On the Action menu, point to Add, then click Project Output.
  7. In the Add Project Output Group dialog box, select your application project, then select Primary output and Content Files and click OK.

The next step is to add a custom action that will be run at the end of installation.

Add the Installer Class as a Custom Action

Custom actions are used to run code at the end of an installation in order to perform actions that cannot be handled during installation. The code for a custom action can be contained in a .dll, .exe, script, or assembly file. For more information on custom actions, see Custom Actions Management in Deployment in the Visual Studio .NET documentation.

To add the Installer class as a custom action

  1. In Solution Explorer, select the Setup project.
  2. On the View menu, point to Editor, then click Custom Actions.

    The Custom Actions Editor opens.

  3. In the Custom Actions Editor, select the Install node.
  4. On the Action menu, choose Add Custom Action.
  5. Double-click the Web Application Folder, then select the Primary output and click OK.
  6. In the Properties window, select the InstallerClass property and make sure that it is set to true.
  7. Select the CustomActionData property and enter the following text: /ServerName=[EDITA1] /ServiceName=[EDITA2]

    The CustomActionData property provides the two parameters that are passed to the custom action, separated by a space.

The next step is to add a user interface to enter the information during installation.

Add a User Interface Dialog Box

User interface dialog boxes are displayed during installation to collect information from the user. For more information on user interface dialog boxes, see User Interface Management in Deployment in the Visual Studio .NET documentation.

To add a custom user interface dialog

  1. In Solution Explorer, select the Setup project.
  2. On the View menu, point to Editor, then click User Interface.
  3. In the User Interface Editor, select the Start node.
  4. On the Action menu, choose Add Dialog.
  5. In the Add Dialog dialog box, choose the Textboxes (A) dialog box, then click OK.
  6. On the Action menu, choose Move Up, and repeat until the Textboxes (A) dialog box is located above the Installation Address dialog box.
  7. In the Properties window, set the following properties:
    Property Value
    BannerText Enter the server name and service name
    Edit1Label Server Name:
    Edit1Value Localhost
    Note   This specifies a default server. You can enter your own default server name here.
    Edit2Label Service Name:
    Edit2Value <name of the service>
    Edit3Visible false
    Edit4Visible false

    Notice that the Edit1Property property is set to "EDITA1" and the Edit2Property property is set to "EDITA2". These correspond with the values that you entered in the CustomActionData property in the Custom Actions Editor. When the user enters text in these edit controls during installation, the values are automatically passed by the CustomActionData property.

Build and Install

The final step is to build the Setup project in order to create the installer, then to install your application on the target server.

To build the Setup project

  • On the Build menu, choose Build Projectname, where Projectname is the name of your Setup project.

To deploy the application to a Web server on your development computer

  • In Solution Explorer, select your Setup project, then on the Project menu, choose Install.

To deploy the application to a Web server on another computer

  1. In Windows Explorer, navigate to your project directory and find the built installer. The default path will be \Documents and settings\Yourloginname\Setupprojectname\Project configuration\Productname.msi. The default Project configuration is Debug.
  2. Copy the .msi file and all other files and subdirectories in the directory to the Web server.
  3. On the Web server, double-click the Setup.msi file to run the installer.

Conclusion

As you can see, redirecting an application to use a different XML Web service is relatively easy. Even though this example uses a Web application that targets an XML Web service, you can easily adapt it for a Windows application. Instead of working with the Web.config file, use the app.config file; use a Setup project rather than a Web Setup project. Other than that, the process is the same.

This exercise has demonstrated a few new techniques: using the URL Behavior property to store information in the configuration file, using Installer classes to perform work during installation, and using the CustomActionData property to store user information during installation.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.