The webservice is calling in a regular interval of time without doing a page postback.
Using the Code
- Create an ASP.NET webservice
- Create an ASP.NET web site
Steps in Details
1. Create an ASP.NET Webservice
Create a new project 'ASP.NET Web Service Application' in Visual Studio.
Open the 'Service1.asmx.cs' file and uncomment the following line from the attribute section of the class '
Service1. This change is for allowing the webservice to be called from script.
By default, you will have a method "
HelloWorld" in the class '
Make the class like below. This method will return the current time in the server.
public string HelloWorld()
TimeSpan time = DateTime.Now.TimeOfDay;
Build the webservice project and we will add this webservice in the ASP.NET website project to check whether the webservice is available to use. I will describe this below.
Now, the webservice is ready.
2. Create an ASP.NET Web Site
In the same project (
WebService Project), add 'New Project -> New Web Site' and choose 'ASP.NET Web Forms Site' and choose 'File System' as the web location.
Test the Web Service Created in the Above Step
Once you create the new website project, then we can test our web service (created in the first step).
- Right click on the website project.
- Add Service Reference.
- Click on 'Discover' button. It will show the webservice in the window. You can use that webservice URL for accessing the service.
- In the browser, access that URL and check whether your webservice is showing '
HelloWorld' method in the browser.
- Open the 'Default.aspx' file and put the following controls in the
<div id="myWebService" style="behavior:url('webservice.htc')" onresult="getResult()"></div>
<asp:Label ID="Result" runat="server">Result will be here.</asp:Label>
Here replace the service URL (http://localhost:46668/Service1.asmx?wsdl[^]) with your webservice URL.
setInterval method is calling the method every 2000 milliseconds (2 seconds).
- Here '
myWebService' is the id of the
- In '
useService' method, the first argument is the webservice URL with
?wsdl in the ending, and the second argument is any name you like.