banner



How To Consume Web Service In C#

Background


In my previous commodity Introduction to Web Service with Example in ASP.Internet we learned how to create a simple Web Service. This commodity explains how to consume the Spider web Service in an ASP.Net spider web Application. And remember, I have written this article but focusing on beginners. So let us start stride-by-step so beginners can understand it very easily.

Requirement

You demand to go along a Web Service application in running mode so it can be attainable for employ, so become to my article Introduction to Spider web Service with Case in ASP.Internet and create a Web Service and keep it in running mode I promise you lot take done that.

What does consuming mean?

Many beginners are confused about what means consuming ways, but its very unproblematic, information technology means to use the Spider web Services in an application.

Example

I take created Web Services and now I want to utilize it in a existent requirement so I used it in an ASP.Net Spider web Awarding. In other words, I am consuming the Spider web Service in an ASP.Net spider web application. Similarly you can use the same Spider web Service in Windows, console application, Coffee and other applications.

I hope you understand the word "consuming".

So permit the states create the simple ASP.Net Spider web Application every bit:

  1. "Start" - "All Programs" - "Microsoft Visual Studio 2010".
  2. "File" - "New" - "Project..." and then in the New Project window "C#" - "Empty Project" (to avoid calculation a chief page).
  3. Give the project a name, such equally "Consumingwebservice" or another equally you wish and specify the location.
  4. Then right-click on Solution Explorer and select "Add New Item" - "Default.aspx" page.
  5. Then drag iii TextBoxes, one Button, and one Label onto the "Default.aspx" page.

And so the <trunk> section of the Default.aspx page looks as in the following:

Default.png

In the preceding source code, you have seen that I accept taken 3 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 Instance in ASP.Cyberspace takes the three input values day, month and yr so I accept taken the three text boxes and button click event. We will call the Spider web Service method and the output volition exist displayed on the label control so I accept also taken one button and label control.

I promise you understand it.

Now your Solution Explorer of ASP.Net Web Awarding will be every bit in the following:

Slnexplorer.png

Adding a Web Service Reference in the ASP.Internet Web Awarding

The about important job when consuming a Spider web Service in an ASP.Net Web Application is adding the Web Service reference into the ASP.Net web application. So how to add information technology? Allow us encounter the procedure.

Correct-click on the ASP.Net Web Application and click on "Add Service Reference" equally in the following:

AddServiRef.png

Then later clicking on the to a higher place pick, the following window will announced, then click on the "Advanced" tab.

Advtab.png

At present afterwards clicking on the Advanced tab, it will testify the post-obit window then click on the "Add together Web Reference" option every bit in the following in a circle:

FAddsevice.png
.
Subsequently clicking on the Add Web Reference tab, it volition testify the following window. At present this is a very important step, when calculation the web reference to the ASP.Net spider web Application. Since you run across "URL" pick in the following window, on that window we demand 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 run across the process over again.

  • Run the Spider web Service we created in my article Introduction to Web Service with Example in ASP.Cyberspace by clicking on F5 or whatever other option you are familiar with, information technology will then show the post-obit web page.

ServiceUrl.png

As yous clearly meet there, in the preceding window, it displays the method named "converttodaysweb" equally we created in our Web Service, now only you demand to copy the preceding URL that I accept circled in cerise and paste information technology into the Step 4 window URL option, then the Pace 4 window will look as in the following:

AddingRef.png

-----What After Pasting the URL in the preceding URL box

Afterward pasting the URL in the preceding window box, click on the light-green right headed pointer button, it volition notice the Web Services available related to that URL address and yous see that in that related URL 1 Spider web Service is found message is displayed along with the Spider web Service proper noun, "Web Services" in the preceding right paw side window.

The Name of the Web Service is "WebService" considering I accept given the class name as Spider web Service, that's why the name is Web Services, in your instance it might exist different or the course name is anything and then you lot tin use any name for Web Service and so don't be confused about it.

  • Spider web Reference Name

In the right hand corner of the window yous have seen the pick for the Web reference name; the spider web reference name is anything you wish and this proper name will be added in your ASP.Internet Spider web Awarding as allies proper noun for Web Service. In my article I have given the web reference name every bit "local host".

And then after calculation the Web Service reference in the ASP.Cyberspace web awarding the Solution Explorer will expect as in the following:

AddedRefinsolu.png

In the preceding window, y'all accept conspicuously seen that the Web Service reference named "localhost" is added into the ASP.Cyberspace web Awarding. I promise you lot empathize how to add together the Web Service reference into the ASP.Net web application.

Calling the Spider web Service method from the ASP.Net Web Awarding

Nosotros take added the Web Service reference into our web application. Now next is how to telephone call the Web Service method that nosotros created in our Web Service Application from the ASP.Net Web Application.

The following is the procedure:

1. Get to the Default.aspx page of our ASP.Net Web application and double-click on the button that we have placed on the Default.aspx folio.

2. At present write the post-obit code in the push button click to create the object of the Web Service class:

localhost.webservice age=new localhost.webservice();

In the code in a higher place, I have created the object of Web Service class named "age" followed by the Web reference proper noun ("localhost") and Spider web Service class ("webservice"), I hope you sympathize how to create the object of the Web Service grade.

The entire code of the Default.aspx.cs page will be as follows:

Cscode.png

Code Explanation

In the code above, I first created the object of the Web Service class named "age" followed by Spider web reference name ("localhost") and Web Service class ("webservice").

And then I declared the three integer variables "day", "calendar month" and "year" to store the values provided by the user every bit input from the Textbox1, Textbox2 and Textbox3.

Now, in the side by side step, as you know our new Web Service method takes iii parameters, so I ed the three input parameters "day", "month" and "yr" to the Web Service method "converttodaysweb".

So I alleged another integer variable, "a", to store the values returned by the Spider web Service method "converttodaysweb".

And finally I displayed the values returned past the Web Service method "converttodaysweb" on the label control using variable "a" because, as yous know, we take stored the returned values of the method into the variable a, and then the terminal result will exist stored in the variable a.

Now, run the ASP.Net web application and provide the input of solar day, month and year. I will enter my Appointment of Birth and and so I volition click on the "Calculate" button, it will show the output as in the following:

outputScreen.png

In the preceding screen, you come across that currently, I am 8702 days sometime, which ways that for the last 8702 days, I have been on this globe.

Notation:

  • For detailed code please download the null file attached higher up.
  • As well refer to my previous article well-nigh creating a Web Service.

Summary

I hope that beginners, too every bit students, understand the creation and consumption of Web Services in ASP.Cyberspace web applications using my two articles. If y'all have whatever suggestion regarding this articles then please contact me. Student suggestions are also welcomed.

How To Consume Web Service In C#,

Source: https://www.c-sharpcorner.com/UploadFile/0c1bb2/consuming-web-service-in-Asp-Net-web-application/

Posted by: sieverswoudde89.blogspot.com

0 Response to "How To Consume Web Service In C#"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel