
Writing the Custom Web Service
This section describes how to write an AJAX-enabled Web service that provides a method that returns the string "Hello World" and the current server time.
To write the custom Web service
In Solution Explorer, right-click the Web site name (http://localhost/HelloWorldService), and then click Add New Item.
Under Visual Studio installed templates, click Web Service, and then in the Name box, type HelloWorld.asmx.
Make sure that the Place code in separate file check box is selected, and then click Add.
Visual Studio 2008 creates a new Web service that consists of two files. The HelloWorld.asmx file is the file that can be invoked to call Web service methods. It points to the Web service code. The code itself is in a class file (HelloWorld.vb or HelloWorld.cs, depending on the programming language) in the App_Code folder. The code file contains a template for a Web service.
Delete the existing code in the class and replace it with the following code:
Imports System
Imports System.Web
Imports System.Collections
Imports System.Web.Services
Imports System.Web.Services.Protocols
Namespace Samples.Aspnet
<WebService([Namespace]:="http://mycompany.org/"), _
WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1), _
System.Web.Script.Services.ScriptService()> _
Public Class HelloWorld
Inherits System.Web.Services.WebService
Public Sub New()
End Sub 'New
'Uncomment the following line if using designed components
'InitializeComponent();
<WebMethod()> _
Public Function Greetings() As String
Dim serverTime As String = _
String.Format("Current date and time: {0}.", DateTime.Now)
Dim greet As String = "Hello World. <br/>" + serverTime
Return greet
End Function 'Greetings
End Class 'HelloWorld
End Namespace
using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
namespace Samples.Aspnet
{
[WebService(Namespace="http://mycompany.org")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// The following attribute allows the service to
// be called from script using ASP.NET AJAX.
[System.Web.Script.Services.ScriptService]
public class HelloWorld : System.Web.Services.WebService
{
public HelloWorld()
{
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public string Greetings()
{
string serverTime =
String.Format("Current date and time: {0}.", DateTime.Now);
string greetings = "Hello World! <br/>" + serverTime;
return greetings;
}
}
}
Notice that the function names are preceded with the WebMethodAttribute attribute as part of the function declaration. In addition, the HelloWorld class is qualified with the ScriptServiceAttribute attribute.
These attributes enable the Web service to be called from script in AJAX-enabled ASP.NET Web pages.
Save the file and close it.
Open the HelloWorld.asmx file and add the following directive:
<%@ WebService Language="VB" CodeBehind="~/App_Code/HelloWorld.vb" Class="Samples.Aspnet.HelloWorld" %>
<%@ WebService Language="C#" CodeBehind="~/App_Code/HelloWorld.cs" Class="Samples.Aspnet.HelloWorld" %>
Save the file and close it.
Now you can test the Web service in your browser. This test does not use script to call the Web service methods. This is only to verify that the Web service is working correctly.
To test the Web service
Open the browser and enter the following URL: http://localhost/HelloWorldService/HelloWorld.asmx
The Web service is invoked and a page appears in the browser that shows the methods that are exposed by the Web service.
Click Greetings. A page appears with the Invoke button.
Click the Invoke button to call the method and see the returned value in XML format.
Close the browser.
You have finished creating the AJAX-enabled Web service.