In this article I will show you how we can Invoke the Web Service methods in our Web application or you can also use in any of the application like windows or console application.
In my last article I created a Web Service, So before going to continue this article first read my last article. How to create a Web Service in Asp.net.
Now Open Visual Studio
File >> New >> Project
Web >> ASP.NET Empty Web Application and give a meaning project name as InvokeDemoWebService.
Right click on Project Folder and Select Add Service References.
Now write the Web Service URL in Address field.
Here I am using : http://localhost:12774/CalculateWebService.asmx?WSDL
Now right click on Project Folder and Add >> New Item and add a Web form to display the results.
Now design the web page as per the requirement.
Code Behind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace InvokeDemoWebService
{
public partial class Calculate : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
CalculateWebService.CalculateWebServiceSoapClient client = new CalculateWebService.CalculateWebServiceSoapClient("CalculateWebServiceSoap");
protected void btnAdd_Click(object sender, EventArgs e)
{
int result = client.Add(Convert.ToInt32(txtFirstNumber.Text), Convert.ToInt32(txtSecondNumber.Text));
lblResult.Text = result.ToString();
}
protected void btnSub_Click(object sender, EventArgs e)
{
int result = client.Sub(Convert.ToInt32(txtFirstNumber.Text), Convert.ToInt32(txtSecondNumber.Text));
lblResult.Text = result.ToString();
}
protected void btnMulti_Click(object sender, EventArgs e)
{
int result = client.Multi(Convert.ToInt32(txtFirstNumber.Text), Convert.ToInt32(txtSecondNumber.Text));
lblResult.Text = result.ToString();
}
protected void btnDiv_Click(object sender, EventArgs e)
{
int result = client.Div(Convert.ToInt32(txtFirstNumber.Text), Convert.ToInt32(txtSecondNumber.Text));
lblResult.Text = result.ToString();
}
}
}
Web.Config
<client>
<endpoint address="http://localhost:12774/CalculateWebService.asmx"
binding="basicHttpBinding" bindingConfiguration="CalculateWebServiceSoap"
contract="CalculateWebService.CalculateWebServiceSoap" name="CalculateWebServiceSoap" />
</client>
Below line is used to call the Web Service:
CalculateWebService.CalculateWebServiceSoapClient client = new CalculateWebService.CalculateWebServiceSoapClient("CalculateWebServiceSoap");
Here the "CalculateWebServiceSoap" is the name of end point from the Web Config file which is automatically generated when we adding the Web Service in our application.
Output:
No comments:
Post a Comment