How to Create web service and calling web services in Asp.net

Опубликовано: 12 Июль 2026
на канале: Amit B Parmar
3,757
40

In this video, I have demonstrated on how to create web service as well as how to consume it on the website.

Free Asp.net Developer Source Create web service and calling web services asp.net

A web service is an entity that you can develop to provide particular functionality over the internet.
▪ For example weather information, live score of cricket, simple and compound interest formula.
▪ For such purposes you make a web service for each. It will useful not only for one website but also for many other websites those are providing the same facilities.
▪ The developer just needs to consume the web services and then its required function is working.
▪ So, it is better to develop a web service common to all instead of writing the same business logic for all websites.
▪ And moreover web service provides common functionalities and results for all websites.
▪ And web service irrespective of any programming language as it is based on XML so it can be useful to all language-based applications.
▪ Web Service messages are formatted as XML, a standard way for communication between two incompatible systems. And this message is sent via HTTP so that they can reach to any machine on the internet without being blocked by the firewall.

Step 1: Right click on WebServicesDemo solution in solution explorer and add new asp.net web application project and name it CalculatorWebApplication.

Step 2: Now we need to add a reference to web service. To achieve this
a) Right click on References folder in the CalculatorWebApplication project and select Add Service Reference option
b) In the Address textbox of the Add Service Reference window, type the web service address and click GO button. In the namespace textbox type CalculatorService and click OK.

Step 3: Right click on CalculatorWebApplication project in solution explorer and add new webform.

Step 3: Copy and paste the following HTML
Get the HTML for the page from my blog as youtube doesn't allows HTML in the description of the video.

Syntax to create web service :

public class WebService1 : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld() { return "Hello World";}
[WebMethod]
public int sum(int a, int b)
{
return a+b;
}
}

Consume web service
• Make an object of web service and you can call any method within that web service.
home.aspx.cs code :

public partial class counter : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e){}
protected void btnAdd_Click(object sender, EventArgs e)
{
WebService1 intsum = new WebService1();
int a = Convert.ToInt32(txt1.Text);
int b = Convert.ToInt32(txt2.Text);
lblSum.Text=intsum.sum(a,b).ToS tring();
}