Practical Approach To ASP.NET Web Services - Part Two - Consuming A Web Service

This article is the second part of the "Practical Approach To ASP.NET Web Services" series. Here, in this article, we will continue with the same example which we had used in our previous article

To consume a web service, all we need to do is to generate a proxy class using the web service WSDL document. WSDL stands for Web Service Description Language. In our previous example, we had built a calculator web service and to WSDL documents of this web service, all we need is to click on Service Description.

web service

When you click on Service Description that will take you to the WSDL document of the URL. So what is WSDL?

web service

WSDL formally defines a web service. What does this WSDL contain? This WSDL contains lots of things, like a list of all the methods that are exposed by web services. In our web service, it exposes one method "Add".

web service

In WSDL document, look at where the "Add" method is present.

web service

Not only method names, but WSDL also specifies the parameters that this method expects. So, here in our Add method, it expects parameters. Here, the parameters are first number and second number. Also, it specifies the name of the parameters and datatype.

web service

As you can see from the above screenshot, it has a name and a datatype. The datatype here for both parameters is integer. Not only this, WSDL also specifies the return type.

 Now, in this web service, we have "Add" method in which we have passed two parameters as first number and second number. We are adding those two numbers and returning an output which is an integer. If you look at the screenshot below, the return type is an integer. 

web service

Now, we will see how to generate a proxy class using WSDL document. First,  we will add a Web application to our project.

web service

We can invoke the web service from any type of application, such as - Web application, Windows application or a Console application. We are not bound to .NET applications only but any application built on any platform, like a JAVA application,  can invoke ASP.NET Web Service.

Now, let’s add a proxy class. Now, we are going to add Service reference.

web service

When we click on "Add Service Reference", we get this screen.

web service

Here, we need to copy the URL of the WSDL document.

web service

Now, click on Go button, as shown in the above screenshot.

web service web service

As you can see, our web service has been detected. Now, it is not mandatory to specify the WSDL. You can get rid of the WSDL part and click on Go button. It will detect your Web Service. 

Now, when we expand the Web Service, we should see our Add method here. 

web service
 
Give a meaningful name to it, as CalculatorService and click OK. Once,you click OK what will happen is that Visual Studio will generate a proxy class for us, based on the WSDL document.

web service

There is a folder called "Service References" under which we have Calculator Service. Now, let's add a form in our web application project.

web service

In this web form, we will add two text-boxes - FirstNumber and SecondNumber, so that when a user inputs two numbers, our web service gets called.

web service

After this, let's add a label to show the output and a button to calculate the sum of the two numbers.

web service

Now, click on "Add" button event handler in cs file. We are going to write some code that will communicate to our Web Service .

web service

The proxy class for our Web Service is -

web service

NOTE - You can see the proxy class of your Web Service by clicking on "Show All files", from the top of the Solution Explorer in VS.

web service

When you click on "Show All Files", it navigates your Web Service to the "references.cs" file.

web service

Clicking on that, you can see your Web Service proxy class and also your method name.

web service

Now, let's go back to our CS page. We need to add our proxy class and specify its instance.

web service

Now, we are invoking the "Add" method that expects two parameters as firstnumber and secondnumber respectively.



Show the output in a label.

web service

Set the web application as your main project and run the application.

web service

As you can see, we have added two numbers and we got the desired output.

Now, if you look at the web application itself, we don’t have the logic of adding two numbers in our CS file. So, where is the addition done? Well, it's done in our Web Service because we did access our Web Service using HTTP protocol and the messages are exchanged in a SOAP format.

But, are we sending a SOAP message request?

web service

NO.

So who is doing the conversion for us?

Now, let's run our Web Service and click on "Add", as shown in the below screenshot.

web service

There is SOAP request and response for message. But, we are not doing that here. So, who is converting our method call to a SOAP request? It is done by our proxy class.



Basically, when this application i.e. .NET client application invokes the Add method of this proxy class, the proxy class takes the .NET integers, prepares a SOAP request for us, and calls our Web Service method.

The web service executes that request of adding two numbers and sends a SOAP response back to the proxy class, not directly to the client application. The proxy class is, then, deserialized it into .NET integer type and hands that out to our client application.

So basically, the process of serialization is used for converting .NET applications to a SOAP message. And, once a response is received from the Web Service, converting that SOAP response to the .NET type (i.e. deserialization) is done by the proxy class. So, all the hard work is done by the proxy class and we consume the Web Service just like any other class in a class library.

Finally -
  1. What is WSDL and what is its purpose?
  2. How is a proxy class generated?
  3. What is the use of the proxy class?
  4. What happens when a Web Service reference is added?

The following paragraph provides answers to all the above questions.

Visual Studio generates a proxy class, using WSDL (Web Service Description Language) which is the document of Web Service. The WSDL document formally defines a Web Service. It contains,

  1. All the methods that are exposed by the Service
  2. The parameters and their return types
  3. The return types of the method

This information is then used by Visual Studio to create the proxy class. The client application then calls the proxy class method and the proxy class serializes the parameters, prepares a SOAP request message, and sends it to the Web Service. The Web Service executes the method and returns a response message to the proxy. The proxy class, then, deserializes the SOAP message and hands it to the client application.

We don’t have to serialize and deserialize .NET CLR objects to and from SOAP format. The proxy class takes care of that and makes the life of a developer much easier.

Up Next
    Ebook Download
    View all
    Learn
    View all