Udostępnij za pośrednictwem


Locating Web Services

 Windows Communication Foundation Services and ADO.NET Data Services

Sometimes you may be both the provider and consumer of a Web service. In this case, you probably know the location and function of the Web service. At other times, you may be accessing a Web service provided by someone else. When this occurs, you may not even know if a Web service that suits your purposes in fact exists.

Web References

To simplify the coding model, applications written in managed code use a Web reference to locally represent each Web service. You add a Web reference to your project by using the Add Web Reference dialog box. This dialog box makes it possible for you to browse your local server, the Microsoft UDDI Directory, and the Internet at large. Web service providers publicize their Web services in Web services directories, which is where Web service consumers search of Web services to consume. For more information, see XML Web Services Directories.

The Add Web Reference dialog box uses the process of Web service discovery to locate eligible Web services on Web sites to which you navigate in the dialog box. For a given address, it will interrogate the Web site using an algorithm designed to locate Web service description documents that adhere to the grammar of the Web Services Description Language (WSDL). For more information, see XML Web Service Discovery and XML Web Service Description.

After you have located a Web service for your application to access by using the Add Web Reference dialog box, clicking the Add Reference button will instruct Visual Studio to download the service description to the local machine and then generate a proxy class for the chosen Web service. The proxy class will contain methods for calling each exposed Web service method both synchronously and asynchronously. This class is contained in the local .wsdl file's code-behind file. For more information, see Web References in Visual Studio and Add Web Reference Dialog Box.

You can use the Web Reference URL property to specify the URL to the Web service. The Add Web Reference sets this property by default to the URL of the Web service you select, which is a static URL. A Web reference can use either a static URL or a dynamic URL.

If you leave the URL Behavior set to the default value of static, the proxy class sets the URL property using a hard-coded URL when you create an instance of the class.

If you set the URL Behavior property of the Web reference to dynamic, the application obtains the URL at run time from the appSettings Element (General Settings Schema) of your application's configuration file. When you specify a dynamic URL after adding a Web reference, Visual Studio updates the proxy class to obtain the URL from the configuration file.

<appSettings>
   <add key="myApplication.myServer.Service1"
        value="http://myServer/myWebService/Service1.asmx"/>
</appSettings>

When you create an instance of a proxy object, you can also set the URL property of the object in your application.

Regardless of which URL the proxy uses, it must be for a Web service that adheres to a WSDL that matches the one used when adding the Web reference.

Alternatively, you can generate a proxy class using a tool (Web services Description Language Tool) that is similar to the one used by Visual Studio to create a proxy class when adding a Web reference. This is necessary when you are unable to access the Web service from the machine on which Visual Studio is installed, such as when the Web service is located on a network that will not be accessible to the client until run time. You then manually add the file that the tool generated to your application project. For more information, see How to: Generate a Web Service Proxy.

See Also

Other Resources

Accessing Web Services in Managed Code

Building XML Web Service Clients

XML Web Service Description