In this asp.net post we will learn how to create a web service and how to run and call web service to asp.net web application using c#.
Web service use the SOAP protocol for interface and communication. Web service is a simple web based application which is hosted on server, we can call just web references to use business logic of web service to our web application.
First create a new web service.



Write the web service code at Service.cs file for multiplication and summation.
public class Service : System.Web.Services.WebService
{
public Service () {//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public string HelloWorld() {
return “Hello World”;
}[WebMethod]
public int Sum(int x, int y, int z)
{
return x + y + z;
}[WebMethod]
public int Multiplication(int x, int y, int z)
{
return x * y * z;
}
}




Now, Create a New ASP.net website to call this web service to asp.net website.






Write below code on button click event for summation and multiplication.
protected void btnSum_Click(object sender, EventArgs e)
{
localhost.Service objservice = new localhost.Service();
int ans = objservice.Sum(Convert.ToInt32(txtA.Text), Convert.ToInt32(txtB.Text), Convert.ToInt32(txtC.Text));
lblans.Text = ans.ToString();
}
protected void btnmultiply_Click(object sender, EventArgs e)
{
localhost.Service objservice = new localhost.Service();
int ans = objservice.Multiplication(Convert.ToInt32(txtA.Text), Convert.ToInt32(txtB.Text), Convert.ToInt32(txtC.Text));
lblans.Text = ans.ToString();
}

