How To Run A Sample Web Service In Asp.net
Background
In my previous article Introduction to Web Service with Example in ASP.Net we learned how to create a uncomplicated Spider web Service. This article explains how to eat the Web Service in an ASP.Internet web Awarding. And remember, I accept written this article only focusing on beginners. Then permit us beginning step-past-step so beginners can understand it very easily.
Requirement
Yous need to keep a Web Service awarding in running mode so it tin be accessible for use, so go to my article Introduction to Web Service with Example in ASP.Net and create a Web Service and keep it in running style I hope you have done that.
What does consuming mean?
Many beginners are confused most what means consuming ways, just its very uncomplicated, it means to use the Web Services in an awarding.
Instance
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.Cyberspace web application. Similarly yous can use the same Web Service in Windows, panel application, Java and other applications.
I hope you understand the give-and-take "consuming".
So allow us create the uncomplicated ASP.Net Web Awarding as:
- "Start" - "All Programs" - "Microsoft Visual Studio 2010".
- "File" - "New" - "Project..." and so in the New Projection window "C#" - "Empty Project" (to avoid adding a master page).
- Requite the project a proper noun, such equally "Consumingwebservice" or another as you wish and specify the location.
- Then right-click on Solution Explorer and select "Add New Item" - "Default.aspx" page.
- Then elevate iii TextBoxes, one Push, and one Label onto the "Default.aspx" page.
Then the <body> section of the Default.aspx page looks as in the post-obit:
In the preceding source code, y'all 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 Spider web Service with Instance in ASP.Net takes the iii input values day, month and year so I have taken the iii text boxes and button click event. We will call the Spider web Service method and the output volition be displayed on the characterization control then I accept also taken one button and label control.
I promise you empathise information technology.
Now your Solution Explorer of ASP.Net Web Application will be as in the following:
Calculation a Web Service Reference in the ASP.Cyberspace Spider web Application
The most important task when consuming a Spider web Service in an ASP.Net Web Application is adding the Web Service reference into the ASP.Cyberspace web application. So how to add it? Let us see the procedure.
Right-click on the ASP.Net Spider web Application and click on "Add Service Reference" as in the post-obit:
Then afterward clicking on the in a higher place option, the post-obit window will appear, and then click on the "Avant-garde" tab.
Now after clicking on the Avant-garde tab, it volition show the post-obit window and then click on the "Add Web Reference" selection as in the post-obit in a circle:
.
After clicking on the Add Web Reference tab, information technology will show the following window. Now this is a very important step, when adding the spider web reference to the ASP.Internet spider web Application. Since yous see "URL" selection in the post-obit window, on that window we need to paste or type the Web Service URL address.
And then how to add together the URL Reference in the preceding URL box, permit us come across the process again.
- Run the Spider web Service we created in my article Introduction to Spider web Service with Example in ASP.Internet by clicking on F5 or whatever other option you are familiar with, information technology volition then prove the following web folio.
Every bit you clearly see in that location, in the preceding window, it displays the method named "converttodaysweb" every bit nosotros created in our Web Service, now only you need to copy the preceding URL that I have circled in cherry-red and paste it into the Step 4 window URL option, then the Step iv window will look as in the following:
-----What After Pasting the URL in the preceding URL box
After pasting the URL in the preceding window box, click on the green correct headed pointer button, it volition discover the Web Services bachelor related to that URL accost and you see that in that related URL one Web Service is institute message is displayed along with the Spider web Service name, "Web Services" in the preceding right hand side window.
The Proper noun of the Web Service is "WebService" because I accept given the class name as Web Service, that's why the name is Web Services, in your instance it might be different or the class proper noun is anything so you tin utilise any proper noun for Web Service so don't exist dislocated about it.
- Web Reference Proper name
In the right hand corner of the window you accept seen the option for the Spider web reference name; the spider web reference name is anything you lot wish and this name will be added in your ASP.Net Web Application as allies name for Spider web Service. In my article I have given the web reference name every bit "local host".
And so later on adding the Spider web Service reference in the ASP.Net web application the Solution Explorer volition await every bit in the post-obit:
In the preceding window, you have conspicuously seen that the Spider web Service reference named "localhost" is added into the ASP.Net web Application. I hope you understand how to add the Web Service reference into the ASP.Net web application.
Calling the Spider web Service method from the ASP.Net Web Application
Nosotros have added the Web Service reference into our web application. Now next is how to call the Spider web Service method that nosotros created in our Web Service Application from the ASP.Internet Web Application.
The following is the process:
1. Go to the Default.aspx page of our ASP.Net Spider web application and double-click on the button that we have placed on the Default.aspx folio.
2. At present write the post-obit lawmaking in the button click to create the object of the Web Service class:
localhost.webservice age=new localhost.webservice();
In the lawmaking above, I have created the object of Web Service class named "age" followed past the Web reference proper noun ("localhost") and Web Service grade ("webservice"), I hope you understand how to create the object of the Web Service course.
The entire code of the Default.aspx.cs folio will be every bit follows:
Code Explanation
In the lawmaking above, I showtime created the object of the Web Service class named "age" followed by Web reference name ("localhost") and Web Service grade ("webservice").
So I alleged the 3 integer variables "day", "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 Spider web Service method takes three parameters, then I ed the three input parameters "twenty-four hours", "month" and "year" to the Web Service method "converttodaysweb".
Then I alleged another integer variable, "a", to shop the values returned by the Web Service method "converttodaysweb".
And finally I displayed the values returned by the Spider web Service method "converttodaysweb" on the label control using variable "a" because, as you know, we accept stored the returned values of the method into the variable a, then the concluding result will be stored in the variable a.
Now, run the ASP.Net web application and provide the input of 24-hour interval, month and twelvemonth. I will enter my Date of Birth then I will click on the "Calculate" button, it will show the output as in the following:
In the preceding screen, you lot encounter that currently, I am 8702 days old, which means that for the last 8702 days, I have been on this earth.
Notation:
- For detailed lawmaking please download the nada file attached above.
- Also refer to my previous commodity about creating a Web Service.
Summary
I hope that beginners, as well as students, empathize the creation and consumption of Web Services in ASP.Cyberspace web applications using my two manufactures. If you lot take any suggestion regarding this articles then please contact me. Student suggestions are also welcomed.
Source: https://www.c-sharpcorner.com/UploadFile/0c1bb2/consuming-web-service-in-Asp-Net-web-application/
Posted by: waldschmidthavemprought.blogspot.com

0 Response to "How To Run A Sample Web Service In Asp.net"
Post a Comment