Share via


Code Model for Accessing Web Services in Managed Code

 Windows Communication Foundation Services and ADO.NET Data Services

A Web service client is any component, service, or application that references and utilizes a Web service. It is worthwhile to note that Web service clients need not be a client-based application, such as a desktop application; in most cases, your Web service clients may be other Web applications or even other Web services.

When accessing Web services in managed code, a proxy class and the .NET Framework handle all of the infrastructure coding. Accessing a Web service follows these basic steps:

  • Locate the Web service you want to access.

  • Create a proxy class for the Web service by adding a Web reference to your project.

  • Reference the proxy class in the client code by including its namespace.

  • Create an instance of the Web service proxy class in the client code.

  • Access the Web service using the methods of the proxy.

For more information, see How to: Access a Web Service in Managed Code or How to: Access a Web Service Asynchronously in Managed Code.

Locating a Web Service and Adding a Web Reference

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 using the Add Web Reference dialog box. This dialog box supports browsing Web addresses for a Web service. For more information, see Locating Web Services and Web References.

Reference the Proxy Class

The generated proxy class has its own namespace associated with it, and you must add the namespace to your client application before you can create an instance of that class. For more information, see using Directive (C#) or References and the Imports Statement (Visual Basic .NET).

Create an Instance of the Proxy Class

Before you can call any of the methods of the proxy class, you must first create an instance of that class. This process does not differ at all from creating an instance of any other class. For more information, see new (C#) or Object Variable Declaration (Visual Basic .NET).

Access the Web Service Using the Proxy

To access a Web service method, your client application invokes either the corresponding synchronous method or asynchronous methods of the proxy object. These methods in turn do the necessary work to remote the call over the wire to call the desired Web service method. By default, the proxy class uses SOAP to access the Web service method, as SOAP supports the richest set of data types of the three supported protocols (HTTP-GET, HTTP-POST, and HTTP-SOAP). For more information, see Web Service Wire Formats.

Summary

Using a proxy class that Visual Studio generated directly from the service description of a Web service, accessing a Web service in managed code is a relatively simple process. The proxy class does the work to transform the method call into a request message and the response message back into a method return value.

The following picture shows the relationship of an application that calls a Web service, which in turn calls another Web service to obtain pricing information. In this example, the Get Current Prices Web service is both a Web service provider and a Web service consumer.

Web Service Clients and Web Services

Animated Web Service Clients

See Also

Other Resources

Accessing Web Services in Managed Code

Building Web Service Clients