Web Services for AJAX Using ASP.NET 2.0 ASMX

Prior to WCF, the Microsoft AJAX framework was integrated with ASP.NET 2.0 ASMX Web services using the System.Web.Extensions assembly. Although I don’t recommend using ASMX Web services for new development (because they are not as flexible or robust as WCF services), enabling AJAX integration support on an existing ASMX Web service application is a trivial task.

ASMX 2.0 services are built using a programming model similar to WCF services, using attributes to define service behavior. The attributes WebServiceBinding, WebService, and WebMethod are used to describe the service, and the base class WebService is used as a base implementation class. The code examples in this section compare ASP.NET 2.0 Web services to WCF services and demonstrate how to enable services with scripts by using attributes defined in System.Web.Extensions.

ASMX 2.0 introduced interface-based programming similar to the approach taken by WCF. Example 2-14 shows a simple interface for DataService using ASMX attributes rather than WCF attributes. Conceptually, this is the same interface used in previous samples, defining web methods within the service binding.

Example 2-14. The WebService and WebMethod attributes are used to define an ASMX Web service (KnowledgeBase.Compatibility/IDataService.cs).

using System;
using System.Web.Services;
using System.Web.Script.Services;

namespace KnowledgeBase.Compatibility
{
    [WebServiceBinding(
        Namespace = "http://knowledgebase/",
        Name = "DataService",
        ConformsTo = WsiProfiles.BasicProfile1_1,
        EmitConformanceClaims = true)]
    // WCF: [ServiceContract(Namespace = "http://knowledgebase/",
    //         Name = "DataService")]
    public interface IDataService
    {
        [WebMethod(MessageName="GET")]
        // WCF: [OperationContract(Action = "Get")]
        DataItem GetData(string catalog, string title);

        [WebMethod(MessageName = "Save")]
        // WCF: [OperationContract(Action = "Save")]
        void SaveData(DataItem data);

        [WebMethod(MessageName = "Delete")]
        // WCF: [OperationContract(Action = "Delete")]
        void Delete(DataItem data);
    }
}

ASMX 2.0 Web services include support for script-enabled access through the System.Web.Extensions assembly. Instead of the behavior-based model that WCF uses, ASMX uses the attributes ScriptService and ScriptMethod. ScriptService enables JavaScript support through a script proxy method on the Web service, and ScriptMethod is used optionally on the Web service method to further define script behavior.

Although ASMX 2.0 supports a pure interface-based model, when using the ScriptService attribute you must define it on the class and you must also explicitly include the WebMethod attribute on all script-enabled methods in the class (not just in the interface). Example 2-15 demonstrates the data service exposed through an ASMX service.

Example 2-15. The script-enabled ASMX 2.0 Web service is defined through the ScriptService, WebService, and WebMethod attributes (KnowledgeBase.Compatibility/DataService.cs).

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Services;
using KnowledgeBase.Implementation;
using System.Web.Script.Services;

namespace KnowledgeBase.Compatibility
{
    [ScriptService]
    [WebService(Namespace = "http://knowledgebase/", Name = "DataService")]
    public class DataService : WebService, KnowledgeBase.Compatibility.IDataService
    {
        [WebMethod(MessageName = "GET")]
        public DataItem GetData(string catalog, string title)
        {
            var dataImplementation = new DataAccess(this.User);
            return dataImplementation.GetData(catalog, title);
        }

        [WebMethod(MessageName = "Save")]
        public void SaveData(DataItem data)
        {
            var dataImplementation = new DataAccess(this.User);
            if (data == null) throw new ArgumentNullException("data");
            if (data.Catalog == null)
                data.Catalog = "Default";
            dataImplementation.SaveData(data);
        }

        [WebMethod(MessageName = "Delete")]
        public void Delete(DataItem data)
        {
            var dataImplementation = new DataAccess(this.User);
            dataImplementation.Delete(data);
        }

        public void CreateData(DataItem entry) {
            throw new NotImplementedException();
        }
    }
}

While ASMX 2.0 Web services give you much of the functionality of WCF, the programming model is not as flexible and requires compiled configuration. You cannot choose to enable or disable script behaviors through configuration, and you cannot use a pure interface-based approach because the WebMethod attribute must be applied to the concrete class to enable script support. Regardless, if you have an existing ASP.NET 2.0 Web service application, you can easily enable script proxies through System.Web.Extensions and the ScriptService attribute.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset