Consuming A Web Service In ASP.NET – Part Two

Before starting this article, you should learn from my previous article,

In  my previous article we learned about how we can create a Web Service; in this part of the series of articles we’ll learn about consuming the web service you need to generate a proxy class using the WSDL (Web Service Description Language) document.

Basically WSDL document defines a Web Service and it contain a list of all the methods exposed by web server. Look in my Web Service, it exposes two methods, first is AddName and second one is Sub.



I’m going to run my previous application to get a WSDL document of this service --  click on Service Description.
 


In the following screenshot “AddName” and “Sub” method is listed here in the WSDL Document and not only that ,along with the list of method name, the WSDL document also specifies the parameter name with data types that this method contains.

In method AddName, there are two parameters so their data types are string. Sub method also has two parameters and their data types are int. So the information present in this WSDL document Visual Studio can generate a proxy class for you.
 


Let’s now see how to generate a proxy class usingthe above WSDL document; flip to Visual Studio. I need to add a web application in my previous solution. Right click on to solution and add a new project.

Now we can invoke a web service with any application from web application, windows application and from console application as well.
 


Now go ahead and add a Asp.Net web application, let’s call it DemoWebService and hit on “Ok” button.
 


So in the following screenshot I have a web application, and we need to add a web service reference. Now let’s generate a proxy class for this. Right click on to References folder and select Add Service Reference.
 


Within this Add service Reference window. In the address bar we need to type the address of the WSDL document. Click on to “Go” button in front of address bar. After clicking on the go button you’ll see that MyWebService is detected. If you expand this web service you’ll get My Service Soap, whenever you select it this exposes two methods called AddName and Sub so give it a meaningful namespace and hit the “Ok” button.
 


Once you click on the ok button then visual studio based on the WSDL document is going to generate a proxy class for you. Look at the solution. A Service Reference folder is there and under it a DemoService namespace which we’ve provided. If you want to see proxy class that is generated for us click on the button which shows all files.
 


Expand your namespace service you’ll find Reference.svcmap, and if you expand it you’ll find Reference.cs file. Open this file.
 


In this file we’ve a class called MyWebServiceClient, so this is the proxy class that is generated for us.
 


If you scroll down you’ll see this class contains two methods AddName and Sub, with their parameters similar to ones we’ve seen in our web service.
 


Now client application, that is our web application DemoWebService, is going to communicate with this proxy class. That proxy class is going to invoke the web service method. For this I need to add a Web Form so right click onto Web application and add a web form.
 


Give it a name to your web form and click on to ok button.
 


Make some design that looks like the following screenshot, after completing the necessary design double click on the button to make some code.
 


In the button event, I’m going to interact with the proxy class that we’ve generated. The name of our proxy class is MyWebServiceSoapClient, this class is presented in DemoService namespace here I need to create an instance of this class.

I’m creating an instance of that class 'cl' and we know that this client instance has got Sub() method.
  1. DemoService.MyWebServiceSoapClient cl = new DemoService.MyWebServiceSoapClient();  
After that I’m going to invoke Sub() method of the proxy class, it takes two parameters.
  1. cl.Sub(Convert.ToInt32(TextBox1.Text), Convert.ToInt32(TextBox2.Text));  
So let's store this result in a int res variable type.
  1. int res = cl.Sub(Convert.ToInt32(TextBox1.Text), Convert.ToInt32(TextBox2.Text));  
  1. protected void Button1_Click(object sender, EventArgs e)  
  2. {  
  3.     DemoService.MyWebServiceSoapClient cl = new DemoService.MyWebServiceSoapClient();  
  4.  
  5.     int res = cl.Sub(Convert.ToInt32(TextBox1.Text), Convert.ToInt32(TextBox2.Text));  

  6.     Label1.Text = res.ToString();  
  7. }   


Now all is set, set this project as startup project.
 


And set this form as start page.
 


Now run your project by pressing F5. We’ll get our result.
 


So in the MyWebForm.aspx.cs, there is no logic of subtracting two numbers so subtraction is done by the web service. All operations are done by the proxy class.

Thanks for reading this article, stay tuned with us for more on Web Services.

Next Recommended Readings