Step 1 : Create the ASP.NET. Web Service Source File.
Open Visual Studio 2010 and create a new web site.->Select .Net Framework 3.5. ->Select ASP.NET Web Service page -> Then, you have to give the name of your service. In this example I am giving it the name "MyApplicationWebService". Then click the OK Button. A screenshot of this activity is shown below.
![img1.gif]()
Step 2 : Click on the "OK" button; you will see the following window.
![img2.gif]()
Here (in the above figure), you will note that there is a predefined method "HelloWorld" which returns the string "Hello World". You can use your own method and can perform other operations.
Here I made a simple method "GetTotalClickCount()" which returns an integer value.
Service.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace MyApplicationWebService
{
/// <summary>
/// Summary description for Service1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public int GetTotalClickCount()
{
int count;
if (Application["ConnectCount"] == null)
count = 1;
else
count = (int)Application["ConnectCount"] + 1;
Application["ConnectCount"] = count;
return count;
}
}
}
Step 3 : Build the Web Service and Run the Web Service for testing by pressing the F5 function key.
![img3.gif]()
Copy the url of this web service for further use.
Step 4 : Click on the "GetPerUserClickCount" Button to test the web service.
![img4.gif]()
Click on Invoke button to test the Web Service.
![img5.gif]()
Refresh the browser or run web service again and again. We will then get:
![img7.gif]()
and so on 4,5,6,7...
Now our web service is ready to use. We just need to create a new web site to consume the web service.
Example of Testing Web Service in .Net
Step 5 : Create a Test Web Site by File > New > Web Site > ASP.Net Web Site.
![img8.gif]()
Give a name to the web site; for example here I have chosen the name "MyTestWebApplication" and clicked on the "ok" button.
Step 6 : Right-click in the Solution Explorer and Choose "Add Web Reference".
![img9.gif]()
Step 7 : Past the url of the web service and click on "Green arrow" button and then "Add reference".
![img10.gif]()
Step 8 : Now your web service is ready for use. You can see it in the Solution Explorer.
![img11.gif]()
Step 9 : Go to the design of the Default.aspx page; drag and drop one TextBox and one Button.
![img12.gif]()
Rename the Button as PressMe.
Step 10 : Go to the Default.cs page and on the button click event use the following code.
protected void Button1_Click(object sender, EventArgs e)
{
localhost.Service1 myservice = new localhost.Service1();
TextBox1.Text=myservice.GetTotalClickCount().ToString();
}
Step 11 : Pressing the F5 function key to run the website, you will see.
![img13.gif]()
Hit the PressMe.
![img14.gif]()
See the output which start from 4 because we already access the page 3 times see step 4.
![img15.gif]()
![img16.gif]()
Resources