Wahlin on .NET

Creating AJAX-Enabled Web Services

Your guide to handling XML parsing issues using JSON.

In my last article, I discussed how Web services can play a critical role in AJAX applications and the role that JSON plays in the message exchange process. In a nutshell, Web services provide a way to significantly reduce the size of messages exchanged between a client and server and allow processing to be offloaded to the client. Several different controls in the ASP.NET AJAX Toolkit, such as the AutoCompleteExtender, leverage Web services extensively.

In this article, I'll focus on how to create Web services that can be consumed by AJAX applications. Future articles will provide additional details on interacting with Web services and discuss alternative techniques, such as Page Methods.

Creating AJAX-Enabled Web services in .NET
Although modern browsers such as Internet Explorer, FireFox and Safari support many different technologies ranging from DOM to CSS to JavaScript, XML parsing support isn't consistent across browsers and is fairly limited except in Internet Explorer. This lack of parsing support presents a challenge when exchanging data between a browser and a service, since Web services typically send XML messages that conform to the SOAP specification. Fortunately, you can overcome the XML parsing problem by exchanging messages using JavaScript Object Notation (JSON), as opposed to SOAP. JSON parsing support is built-in to all major browsers, making it a viable option. A little work is required on your part to allow .NET Web services to handle JSON messages, though.

The code below shows a standard .NET Web Service with a single Web Method named Add(). While this service will work fine with clients that are capable of working with SOAP, it won't work "out of the box" with ASP.NET AJAX applications since it doesn't know how to work with JSON messages.

[WebService(Namespace = "http://www.xmlforasp.net")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class MathService : System.Web.Services.WebService
{

    public MathService()
    {

    }

    [WebMethod]
    public int Add(int x, int y)
    {
        return x + y;
    }

}

To allow the serviceto handle JSON messages, the ASP.NET AJAX Extensions's ScriptServiceAttribute class can be applied to the Web Service class, as shown in the code below. The attribute is applied directly above the service's class and does nothing more than mark the service as being JSON-capable.

[System.Web.Script.Services.ScriptService] 
[WebService(Namespace = "http://www.xmlforasp.net")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class MathService : System.Web.Services.WebService
{

    public MathService()
    {

    }

    [WebMethod]
    public int Add(int x, int y)
    {
        return x + y;
    }

}

Several other classes are used behind the scenes in ASP.NET AJAX applications to help out with JSON messaging in Web services including ScriptHandlerFactory and ScriptModule. These classes are configured in web.config when you create an ASP.NET AJAX-Enabled Web site in Visual Studio .NET (see Listing 1). Looking through the listing you'll see that all calls to .asmx files are routed through the ScriptHandlerFactory HttpHandler. ASP.NET AJAX authentication and profile services (topics I'll discuss in future articles) also rely on this HttpHandler.

Although the ASP.NET AJAX UpdatePanel control offers a simple way to AJAX-enable a Web site, by using Web services you can offload much of the work to the client and format data directly in the browser instead of sending formatted HTML between the browser and server. In the next article, I'll discuss how JavaScript proxy objects can be created using ASP.NET AJAX to consume Web services and exchange JSON messages.

About the Author

Dan Wahlin (Microsoft MVP for ASP.NET and XML Web Services) is the founder of The Wahlin Group which specializes in .NET and SharePoint onsite, online and video training and consulting solutions. Dan also founded the XML for ASP.NET Developers Web site, which focuses on using ASP.NET, XML, AJAX, Silverlight and Web Services in Microsoft's .NET platform. He's also on the INETA Speaker's Bureau and speaks at conferences and user groups around the world. Dan has written several books on .NET including "Professional Silverlight 2 for ASP.NET Developers," "Professional ASP.NET 3.5 AJAX, ASP.NET 2.0 MVP Hacks and Tips," and "XML for ASP.NET Developers." Read Dan's blog here.

comments powered by Disqus
Most   Popular
Upcoming Events

.NET Insight

Sign up for our newsletter.

Terms and Privacy Policy consent

I agree to this site's Privacy Policy.