0% found this document useful (0 votes)
3 views23 pages

Lect 13-Web Services

The document provides a comprehensive guide on creating and consuming web services using Visual Studio 2010. It explains the definition of web services, their interoperability, and step-by-step instructions for creating a web service project, including coding examples. Additionally, it outlines the process for consuming the created web service in an application, culminating in a successful implementation demonstration.

Uploaded by

Alishba Haroon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views23 pages

Lect 13-Web Services

The document provides a comprehensive guide on creating and consuming web services using Visual Studio 2010. It explains the definition of web services, their interoperability, and step-by-step instructions for creating a web service project, including coding examples. Additionally, it outlines the process for consuming the created web service in an application, culminating in a successful implementation demonstration.

Uploaded by

Alishba Haroon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Creating Web Services in Visual Studio 2010

Q. What is Web Service?

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:

1. Open Visual Studio 2010


2. Click on New project Link on start page, New Project Window Will Open
3. Select Asp.net Web Application in the template window as shown below

Choose Framework 3.5


Choose ASP.Net Web Service

4. Enter the name of project or leave as it is and Press OK


5. Open Solution Explorer of your project and Right click on project, the pop window will
open, Drag cursor on to add menu, the submenu of add menu will open, click on
NewItem or Go to Add->NewItem.The AddNew Item Window will open, Select
WebService and Rename WebService1.asmx to MyWebService.asmx, then click on
ok button. These steps are shown in the figure below
6. The MyWebService.asmx file will appear in the solution explorer, open
MyWebService.asmx file
7. Declare your own method in this file bellow the HellowWorld function. You can remove
the HellowWorld function from the WebService.

For Example : the bellow function generate a random number and returns to it.

[WebMethod(Description = "Get Random Number")]//Description about web method


public string GetNumber() // Declaration of Method
{
Random RM=new Random();
return RM.Next(1,100).ToString();
}
[WebMethod(Description = "Adds Two Number And return result")]
public string Add(int firstno, int secondno)
{
double result = firstno + secondno;
return result.ToString();
}
Code in text format is shown here
using System;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Web;
usingSystem.Web.Services;

///<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 () {

//Uncomment the following line if using designed components


//InitializeComponent();
}

[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.

You will see the browser


2. Copy the URL adder from the URL of browser, i.e.

http://localhost:17512/WebSite7/MyWebService.asmx
3. Add new project in the existing solution as shown

4. You will see the newly added project as shown


5. Right click on your newly added project, click Add Service Reference , the Service
Reference Window will open.
6. Past the copied URL address in the address textbox of service reference window. click to
'Go' button
7. Rename the Namespace as required and then Press ok.

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

Congratulations! You have successfully implemented the Web Services in ASP.Net


Further Readings

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

You might also like