Wednesday, June 10, 2015

How to Consume the Web Service..

What does consuming mean?

Many beginners are confused about what means consuming means, but its very simple, it means to use the Web Services in an application.

Example:
I have created Web Services and now I want to use it in a real requirement so I used it in an ASP.Net Web Application. In other words, I am consuming the Web Service in an ASP.Net web application. Similarly you can use the same Web Service in Windows, console application, Java and other applications.
I hope you understand the word "consuming".

So let us create the simple ASP.Net Web Application as:
  1. "Start" - "All Programs" - "Microsoft Visual Studio 2010".
  2. "File" - "New" - "Project..." then in the New Project window "C#" - "Empty Project" (to avoid adding a master page).
  3. Give the project a name, such as "Consumingwebservice" or another as you wish and specify the location.
  4. Then right-click on Solution Explorer and select "Add New Item" - "Default.aspx" page.
  5. Then drag three Text Boxes, one Button, and one Label onto the "Default.aspx" page.
Then the <body> section of the Default.aspx page looks as in the following:
Default.png
 
In the preceding source code, you have seen that I have taken three text boxes to get input from users because we know that our Web Service method created as in my article Introduction to Web Service with Example in ASP.Net  takes the three input values day, month and year so I have taken the three text boxes and button click event. We will call the Web Service method and the output will be displayed on the label control so I have also taken one button and label control.
I hope you understand it.

Now your Solution Explorer of ASP.Net Web Application will be as in the following:
Slnexplorer.png 

Adding a Web Service Reference in the ASP.Net Web Application 

The most important task whem consuming a Web Service in an ASP.Net Web Application is adding the Web Service reference into the ASP.Net web application. So how to add it? Let us see the procedure.

1. Right-click on the ASP.Net Web Application and click on "Add Service Reference" as in the following:
AddServiRef.png 

2.  Then after clicking on the above option, the following window will appear, then click on the "Advanced" tab.
Advtab.png

3. Now after clicking on the Advanced tab, it will show the following window then click on the "Add Web Reference" option as in the following in a circle:
FAddsevice.png 
.
4. After clicking on the Add Web Reference tab, it will show the following window. Now this is a very important step, when adding the web reference to the ASP.Net web Application. Since you see "URL" option in the following window, on that window we need to paste or type the Web Service URL address.

RefScreen.png

So how to add the URL Reference in the preceding URL box, let us see the procedure again.
ServiceUrl.png
As you clearly see there, in the preceding window, it displays the method named "converttodaysweb" as we created in our Web Service, now just you need to copy the preceding URL that I have circled in red and paste it into the Step 4 window URL option, then the Step 4 window will look as in the following:
AddingRef.png

No comments:

Post a Comment