How To Use Configuration Files to Specify the URL That Is Used to Locate a Web Service (316928)



The information in this article applies to:

  • Microsoft .NET Framework 1.1
  • Microsoft .NET Framework 1.0
  • Microsoft Visual Studio .NET (2003), Enterprise Architect Edition
  • Microsoft Visual Studio .NET (2003), Enterprise Developer Edition
  • Microsoft Visual Studio .NET (2003), Professional Edition
  • Microsoft Visual Studio .NET (2003), Academic Edition
  • Microsoft Visual Studio .NET (2002), Enterprise Architect Edition
  • Microsoft Visual Studio .NET (2002), Enterprise Developer Edition
  • Microsoft Visual Studio .NET (2002), Professional Edition
  • Microsoft Visual Studio .NET (2002), Academic Edition
  • Microsoft Web Services (included with the .NET Framework)

This article was previously published under Q316928

SUMMARY

This step-by-step article describes how to use configuration files to specify the URL that is used to locate a Web service.

back to the top

MORE INFORMATION

In a Visual Studio .NET Windows application or in a Web application, the Add Web Reference dialog box helps you to create a reference to a Web service. The Add Web Reference dialog box permits you to specify the URL to a Web service (such as an .asmx page), and then Visual Studio .NET can build the Web reference.

The Web reference has many attributes, including the URL of the Web service. Typically, this attribute information is stored in the compiled application. However, the URL can be stored in a configuration file. In a Visual Studio .NET Windows application, the URL is stored in the App.config file. In a Web application, the URL is stored in the Web.config file. If the URL information is stored in a configuration file, this permits the system administrator to modify the URL, and the programmer does not have to recompile the application.

back to the top

Create the Web Service

  1. Start Visual Studio .NET.
  2. Create a new Microsoft Visual Basic .NET Web service project.

    By default, the Service1.asmx file is created.
  3. In the Code Window, remove the comment for the HelloWorld WebMethod.
  4. On the Build menu, click Build Solution.
back to the top

Use the Web Service

  1. Start Visual Studio .NET.
  2. Create a new Visual Basic .NET Windows application project.
  3. Add a Web reference to the Web service that was created in the "Create the Web Service" section.
  4. In the Solution Explorer window under the Web Reference section, select the Web reference that you added in step 3.
  5. On the View menu, click Properties Window.
  6. In the Properties window, change the URL Behavior property from Static to Dynamic.
  7. On the Build menu, click Build Solution.
back to the top

Check the Configuration File

  1. Open the App.config file of the project or the Web.config file of the project to view the results.
  2. Locate the "appSettings" section that appears as follows:
    <appSettings>
    <add key="Test.localhost.Service1" value="http://localhost/WebService1/Service1.asmx"/>
    </appSettings>
    Note The actual key and the actual value may differ. This depends on the project name that you provide. The key is the reference that is used in your application to refer to this service, and the value is the URL to the Web service. For example, in Visual Basic .NET, to declare a variable as an instance of this Web service, you can use the following code:
    Dim objMyService As Test.localhost.Service1
    This key has three sections that are separated by periods:
    • The namespace for the application project
    • The folder name for the Web reference
    • The class name of the Web service
To understand the entry in the App.config file, follow these steps:
  1. In Microsoft Visual Studio. NET IDE in the Solution Explorer window, right-click Project, and then select Properties.

    The namespace is listed in the Properties dialog box.
  2. In Visual Studio. NET IDE in the Solution Explorer window, right-click Web Reference, and then select Properties.

    The folder name appears in the Properties dialog box.
  3. In Visual Studio .NET IDE in the Solution Explorer window, right-click Web Reference, and then select Properties.

    In the source code of the Web service that is the code behind the window for the .asmx page, the service is implemented as a procedure inside a class. This is the class name. The application obtains this class name through the service name attribute in the .wsdl file in the Web reference.
back to the top

Troubleshoot

You must remember the following points when you use the URL Behavior property:
  • Like all modifications to the Web.config file, if you change the URL, this causes the application to restart.

    Note This only applies to Web applications.
  • If you change the behavior of the Web reference from Dynamic to Static, the key and value pair in the App.config file or in the Web.config file may not be deleted. You must edit the file to verify that the entry is deleted.
  • If the Web reference already has an entry in the App.config file or in the Web.config file, and you make a modification that affects the entry (such as changing the folder name for the Web reference), the config file may not be updated. You can edit the config file, and then type the new data. Or, you can delete the entry from the file, and then toggle the Web reference behavior to make the entry regenerate.
  • If you do not see an App.config file in the application directory, toggle the URL Behavior property. After you toggle the URL Behavior property, the App.config file is created.
back to the top

REFERENCES

For additional information about how to locate XML Web services, visit the following Microsoft Developer Network (MSDN) Web site:For additional information about how to redirect an application to target a different XML Web service during installation, visit the following Microsoft Developer Network (MSDN) Web site: back to the top

Modification Type:MinorLast Reviewed:7/13/2004
Keywords:kbHOWTOmaster kbConfig kbDeployment kbinfo kbSysAdmin kbWebServices KB316928 kbAudDeveloper kbAudITPRO