Lect 13-Web Services
Lect 13-Web Services
Web services are client and server applications that communicate over the World Wide
Web’s (WWW) Hyper Text Transfer Protocol (HTTP). As described by the World Wide
Web Consortium (W3C), web services provide a standard means of interoperating
between software applications running on a variety of platforms and frameworks. Web
services are characterized by their great interoperability and extensibility, as well as their
machine-processable descriptions, thanks to the use of XML. Web services can be
combined in a loosely coupled way to achieve complex operations. Programs providing
simple services can interact with each other to deliver sophisticated added-value services.
One big feature of Web Services is, Web Services are able to communicate with
heterogeneous applications, because Web Services present the information in XML
format.
To create a Web Service in Visual Studio 2010, we have to follow these steps:
For Example : the bellow function generate a random number and returns to it.
///<summary>
/// Summary description for MyWebService
///</summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX,
uncomment the following line.
// [System.Web.Script.Services.ScriptService]
publicclassMyWebService : System.Web.Services.WebService {
publicMyWebService () {
[WebMethod]
publicstringHelloWorld() {
return"Hello World";
}
[WebMethod(Description = "Get Random Number")]//Description about web
method
publicstringGetNumber() // Declaration of Method
{
Random RM = newRandom();
returnRM.Next(1, 100).ToString();
}
[WebMethod(Description = "Adds Two Number And return result")]
publicstring Add(intfirstno, intsecondno)
{
double result = firstno + secondno;
returnresult.ToString();
}
This is the all about create web service in your project, and then we will discuss about how to use
or consume web service in your application.
Consume Web Service
1. Save all and then Right click on your WebServicei.e MyService.asmx and click on
Browse With. The Browser with window will open, select your installed explorer, for
example select Internet Explorer.
http://localhost:17512/WebSite7/MyWebService.asmx
3. Add new project in the existing solution as shown
8. The reference of the Webservice will be appear in the Service Reference folder in
solution explorer.
9. Go to the default page of your project, take one label, set the text to 'Random Number'.
10. Take another label control set this text as '0' and id as 'lblRandom'.
11. Now, again
Click the Add Reference Button
12. Now, put the following code in the page load event
Finally, Run the default.aspx. you will see the output
http://www.c-sharpcorner.com/UploadFile/e95fe7/using-web-services-in-Asp-Net/
http://www.codeproject.com/Articles/337535/Understanding-the-Basics-of-Web-Service-in-
ASP-NET