Create and Call Web Service in ASP.Net using C#

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.

Create a web service in ASP.Net C#
Create a web service in ASP.Net using C#

 

web service2
Create a web service in ASP.Net using C#

 

Create a web service in ASP.Net using C#
Create a web service in ASP.Net using C#

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;
}
}

 

web service3
Create a web service in ASP.Net using C#
web service4
Create a web service in ASP.Net using C#
Create a web service in ASP.Net C#
Create a web service in ASP.Net using C#
Create a web service in ASP.Net using C#
Create a web service in ASP.Net using C#

 

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

Call web service to ASP.Net website using C#
Call web service to ASP.Net website using C#
web2
Call web service to ASP.Net website using C#
web3
Call web service to ASP.Net website using C#
web4
Call web service to ASP.Net website using C#
web5
Call web service to ASP.Net website using C#
web6
Call web service to ASP.Net website using C#

 

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();
}

 

web7
Call web service to ASP.Net website using C#
web8
Call web service to ASP.Net website using C#

Leave a Reply

Your email address will not be published. Required fields are marked *