In part 1 of 3, of this tutorial we will learn how an ASP.NET AJAX Web Service handler is configured, how a Web Service request message is serialized to JSON, how to create AJAX-enabled Web Services and how to use the ScriptService and ScriptMethod attribute.

In this tutorial we will explore how Web services are an essential part of the .NET Framework for providing cross-platform solutions for exchanging data between distributed systems. Instead of resorting to postback operations, the Web services are used to dynamically inject data into an ASP.NET AJAX page or send data from a page to a back-end system.

Within the ASP.NET AJAX Toolkit there is functionality available through ASP.NET AJAX Extensions as well as a Web service control called AutoCompleteExtender that we will focus on in this tutorial.

The web.config file has a number of new additions that you may be unfamiliar with in Visual Studio 2008. Certain map the prefix to ASP.NET AJAX controls so that they can be used in pages while others define required and . The default HttpHandler used to process .asmx (Active Server Methods) calls is removed and replaced with a ScriptHandlerFactory class located in System.Web.Extensions.dll assembly, which contains all the core functionality used by ASP.NET AJAX.

This is an ASP.NET AJAX Web Service Handler Configuration

This httpHandler replacement is made in order to allow JavaScript Object Notation (JSON) calls to be made from ASP.NET AJAX pages to .NET Web services using a JavaScript Web Service Proxy. Instead of using the SOAP protocol, ASP.NET AJAX sends JSON messages to Web services that results in smaller request and response messages overall. This is more efficient client-side processing of data due to ASP.NET AJAX JavaScript library which is optimized to work with JSON objects.

Learn How a Web Service Request Message Serialized to JSON

Learn How a Web Service Response Message Serialized to JSON

Above is an example of a Web service request and response messages serialized to JSON format. In the first case University passes a class parameter with a value of “Calculus”. In the second block an array of Student objects and their properties.

Learn How to Create AJAX-Enabled Web Services

There are several ways to call Web services within ASP.NET AJAX framework. There is either the AutoCompleteExtender in the Toolkit or using JavaScript. It is pretty straight forward in creating AJAX-enabled services because .Net framework has supported Web services since 2002 and the ASP.NET AJAX Extensions has been built upon. Below is an example of using WebMethod attribute in a Web service:

As shown above the GetStudentsByCountry() method accepts a university parameter and returns a Student object array. The university value passed into the method is forwarded to a business layer class which in turn calls a data layer class to retrieve the data from the database, then fills the Student object properties with data and returns the array.

Learn How to Use the ScriptService Atrribute

When adding the WebMethod attribute it allows the GetStudentsByUnivserity() method to be called by clients that send standard SOAP messages to the Web Service. This doesn’t allow the JSON calls to be made from the default setting in ASP.NET AJAX applications. Instead, to allow the JSON calls to be made you will need to apply the ASP.NET AJAX Extension’s ScriptService attribute to the Web service class. By doing this it enables a Web service to send a response message forward using JSON and also allows the client-side script to call a service by sending JSON messages. Below is an example of a service class called StudentsService that apply’s the ScriptService attribute.

Learn How to Use the ScriptMethod Attribute

The only ASP.NET AJAX attribute that has to be defined in a .NET Web service is the ScriptService attribute in order for it to be used by ASP.NET AJAX pages. Although a ScriptMethod attribute can be applied directly to Web method in a service. A ScriptMethod defines three properties; UseHttpGet, ResponseFormat, XmlSerializeString.

Using the ScriptMethod attribute with the UseHttpGet property can be used to accept GET request instead of POST requests. These requests are sent using a URL with Web Method input parameters converted to QueryString parameters. Here is an example:

Ok, we know now that the Web Methods can accept HTTP GET requests. Additionally, the ScriptMethod attribute can be used when XML responses need to be returned from a service rather than JSON. Let’s say, a Web Service retrieves an RSS Feed from a remote site and returns it as an XMLDocument or XMLElement object, then the processin gfo the XML data can then occur on the client. Below illustrates using the ResponseFormat property to specify that XML data should be returned from a web Method.

The ResponseFormat property can also be used with the XMLSerializeString property which has a default value of false and can return all types except for strings. Strings that are returned from a Web method are serialized as XML when the ResponseFormat property is set to responseFormat.XML. However, when the XMLSerializeString is set to true, every type return from the Web Method is serialized as XML. Here is an example of an XMLSerializeString property to force strings to be serialized as XML.

Here is an example of the value returned from calling the GetXmlStrng Web Method: