Welcome Guest!
Create Account | Login
Locator+ Code:

Search:
FTPOnline Channels Conferences Resources Hot Topics Partner Sites Magazines About FTP RSS 2.0 Feed

Click here to receive your FREE subscription to Visual Studio Magazine

email article
printer friendly

Creating AJAX-Enabled Web Services
Your guide to handling the XML parsing issues by using JSON.
by Dan Wahlin

July 30, 2007

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.

ADVERTISEMENT

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 Most Valuable Professional for ASP.NET and XML Web services) is a .NET development instructor and consultant at Interface Technical Training. Dan founded the XML for ASP.NET Developers Web site, which focuses on using ASP.NET, XML, AJAX and Web services in Microsoft's .NET platform. He’s also on the INETA Speaker's Bureau and speaks at several conferences. Dan has co-authored/authored several different books on .NET, including ASP.NET 2.0 MVP Hacks, Professional ASP.NET AJAX and XML for ASP.NET Developers. When he's not writing code, articles or books, Dan enjoys writing and recording music and playing golf and basketball with his wife and kids. Dan blogs at http://weblogs.asp.net/dwahlin and http://blogs.interfacett.com/dan-wahlins-blog.




Back to top













Java Pro | Visual Studio Magazine | Windows Server System Magazine
.NET Magazine | Enterprise Architect | XML & Web Services Magazine
VSLive! | Thunder Lizard Events | Discussions | Newsletters | FTPOnline Home