Creating web service in ASP.NET using Visual studio 2005 is very easy. Follow this steps:
Open Visual Studio and click New Web Site, and select ASP.NET web service.

After a second your project is created. You will find file named: “Service.cs” in your App_Code folder.
There is already default web methid implemented “Hello World”. Here is how it looks like:
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
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";
}
}
we will change this method “HelloWorld” with an other method “IntigerAddition”. So copy/paste this code into Service.cs file.
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
public Service () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public Int32 IntigerAddition(int a, int b)
{
return a + b;
}
}
It is always useful to try to build your code. If everything is fine, let’s see how it works.
Right click on Service.asmx file in solution explorer and select “View in browser”. The same is if you try to click on “F5″. If there is no web.config file, you’ll be asked to put it into solution.
when web page is opened, you will see listed all services you have. For now it is only “Integer Addition” and if you click on it, you will see SOAP 1.1 and SOAP 1.2 request and response. Also you can test your web service method there if you want.
Now, get us back to solution explorer and create one web page to test this web service.
In Solution Explorer right click on solution and select Add new web site. This is image preview how to do this:

ASP.NET Web service - ASMX
I’ll call this new web site project:
“DemoClientForWebService”.
In your new web site project, ther is a file Default.aspx. Open it and add 2 text boxes, one Button and one more text box. Like this:
Now, we need to add web reference in our web site from web service project. Right click on web site project and select Add Web Reference.
Right click on web project and select “Add web reference”.

Then you need to specify web reference. Select “Web service in this solution”.

Then select your reference and click on button “Add reference”.

Now let’s use this reference and connect our application to web service.
Here is the code for Default.aspx page. This is code behind, file: Deafult.aspx.cs:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using localhost; //this is how we called our web service
public partial class _Default : System.Web.UI.Page
{
localhost.Service myWeb;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
myWeb = new Service();
}
}
protected void bntGo_Click(object sender, EventArgs e)
{
txtResult.Text = myWeb.IntigerAddition(
Convert.ToInt32(txtAdd1.Text),
Convert.ToInt32(txtAdd2.Text)
).ToString();
}
}
And now, let’s do some testing
But first be carefull. You must start your web service first.
It must be running when you try to call it from your web apps.
Recent Comments