Who can consume a web service?
A web service can be consumed by a client application. Different types of client applications can comsume a web service. In today's software environment, almost every application need a web service to enhance its functionality. The important advantage of a web service is that it returns its results in xml format, which can be consumed by different types of clients like browser based clients, rich desktop clients, spreadsheets, wireless devices, interactive voice response(IVR) systems and other business applications.
A client application discovers an web service, and then uses services provided by the web service. This process is known as consuming a Web service. In this article, we learn how to
Creating a client application
Creating a proxy
Consuming the web service using the proxy
Creating a client application
The client application we will create is a web application with the capability to consume a web service. The first step is Click File->New-> Web site. This action displays the New web site dialog box. Select visual Basic in the language combo box and select ASP.NET Web site from the Templates pane. Change the location of the application to http://localhost/HelloWorldWebApp. Click OK button. When we select the ASP.NET web site template, Visual studio.NET automatically adds project reference and files that can be used as the starting point for the web application.
With the above steps, a web form with an .aspx extension is created. From the toolbox, click the Label control, place it on the form and set the text property of the control to lblname.
Before we go further, we need to understand the purpose of a proxy class and how to create a proxy class.
Why to create a Proxy class?
For a client to consume a Web service, the client must know;
Where the Web service is located
How to invoke the methods of a web service
Creating a soap message
A proxy class is a local representation (on the client computer) which perform the above tasks. So,
Proxy classes serve as an intermediate step between the web page running on the client and the actual Web service on the web server. The proxy class is created from the wsdl document by the client application developer. In short, the Web service's WSDL document contains the information to create proxy class. The proxy class helps the client application to use all the services provided by the web service.
What contains proxy class?
The proxy class of an XML web service contains instructions for calling all the XML Web service methods. The proxy also contains code that allows client application to interface with the SOAP runtime located on the web host. The physical representation of the proxy class is the .wsdl file.
How to create a Proxy class in .Net?
Right-click the client web application project in the solution explorer window and select Add Web reference from the shortcut menu as shown in below picture.
The Web reference dialog box is displayed. This dialog box enables us to browse the local server, the Microsoft UDDI Directory, and the Web to locate a Web service provided we are connected to the internet. The Add Web Reference dialog box requests the URL of the web site on which Web services are located. The Add Web Reference dialog box uses the Web service discovery mechanism to locate XML Web services on the Web sites that we enter. We will find there are three hyperlinks on the left pane:
one to the UDDI (Universal Description Discovery Integration) Directory,
two Microsoft's UDDI,
three is Web References on the local machine.
The first two links lead to directories of webservices on Microsoft sites. The first one is intended for real, practical web services and the second one for testing purposes. In the address text box, type the path of the Web service.
if this web site does not contain any .disco files, a message is displayed that The HTML document does not contain Web service discovery information.
After you enter the URL of the site and Click the Go button to locate the description file (WSDL) for the web service. This dialog box displays the descriptions of the XML web services available on the site as shown in the below picture.
It displays information about the methods that the XML web services provides. The details about the methods such as parameters can be viewed by clicking a method in the dialog box.
Select the web service and click the Add Reference button to add the Web reference to the project and close the Add Web Reference dialog box. Visual Studio.Net adds a Web References node, which has the same name as the computer com.jobsken.www that provides the XML Web service to your client application. We can rename the Web reference to change the namespace in which the proxy class is created. We change com.jobsken.www to myRef in the dialog box.
When we click the Add Reference, Visual Studio.Net downloads the service description to the local computer and will automatically create the Proxy class and compile it. And also Visual Studio.Net adds the following files to the client application.
To view the .wsdl file that Visual Studio.net creates, expand the Web references node under your project in the solution explorer.
Note about a web reference:
A Web reference is a pointer to the Web service's WSDL document. As we have discussed earlier the wsdl document describes the web service's API. The .net framework processes this document and generates a proxy class.
Note that for a file to be accessed programmatically, it has to exist as a binary. A web reference registers the proxy class as a binary.
To be able to use the Web service that contains a method for displaying helloWorld string, we must include in the project, a reference to the Helloworld service web service.
Consuming the web service using the proxy
The first step is to create an object of web service proxy. Double click on the web form to add the code required to display the output of the web service. For this, add the following code in the webform.
Protected Sub Page_Load(ByVal sender As Object,
ByVal e As System.EventArgs) Handles Me.Load
'Create an instance of the Proxy class
Dim consumeWebService As myRef.myworld
consumeWebService = New myRef.myworld
lblname.Text = consumeWebService.HelloWorld()
The above code creates an instance of the proxy class for the Web service and display the string hello world in to the label control.
To reference a proxy class in the code, we need to give the fully qualified name of the proxy class. We create an object of the proxy class by using the new operator. The object of the proxy class consumeWebService allows to invoke the HelloWorld() method of the web service. If you run the application by pressing F5, the Hello world message is displayed in the label control and show it on the browser.
With this step, we know how to consume the web service in the client web application which is located on the localhost. Once the application works, we can upload the web application. The web application consumes the web service on every page request.