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

Featured

  • Compare New GitHub Copilot Free Plan for Visual Studio/VS Code to Paid Plans

    The free plan restricts the number of completions, chat requests and access to AI models, being suitable for occasional users and small projects.

  • Diving Deep into .NET MAUI

    Ever since someone figured out that fiddling bits results in source code, developers have sought one codebase for all types of apps on all platforms, with Microsoft's latest attempt to further that effort being .NET MAUI.

  • Copilot AI Boosts Abound in New VS Code v1.96

    Microsoft improved on its new "Copilot Edit" functionality in the latest release of Visual Studio Code, v1.96, its open-source based code editor that has become the most popular in the world according to many surveys.

  • AdaBoost Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the AdaBoost.R2 algorithm for regression problems (where the goal is to predict a single numeric value). The implementation follows the original source research paper closely, so you can use it as a guide for customization for specific scenarios.

  • Versioning and Documenting ASP.NET Core Services

    Building an API with ASP.NET Core is only half the job. If your API is going to live more than one release cycle, you're going to need to version it. If you have other people building clients for it, you're going to need to document it.

Subscribe on YouTube