Cappuccino Javascript Framework – Part 1

Consuming .NET JSON WebService – Part 1

The .NET WebService

On this post I’ll show you HOW-TO develop and consuming JSON Web Services using the Cappuccino Javascript Framework and .Net 2.0 / 3.5 Framework.

In first step, let’s build the JSON .Net WebService, using Microsoft or Mono Framework. To simplify this post I’ll not use the code behind.

Notes:
As we know, to use JSON on .NET WebServices we need to use the following assembly “Microsoft.Web.Extensions.dll”, and in the code we need to define the following namespaces: System.Web.Script.Services, System.Web.Script.Serialization.

Ok, let’s start writing the .Net JSON WebService (XmlJson.asmx) code:

<%@ WebService Language="C#" Class="Services.XmlJson" %>
using System;
using System.Globalization;
using System.Collections;
using System.Data;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;

// Script namespace from Microsoft.Web.Extensions.dll assembly
using System.Web.Script.Services;
using System.Web.Script.Serialization;

namespace Services
{

    /// <summary>
    /// Summary description for WebService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

    // To allow this Web Service to be called from script, using ASP.NET AJAX. 
    [System.Web.Script.Services.ScriptService]
    public class XmlJson : System.Web.Services.WebService
    {

        private Contact _Contact;

        public XmlJson() { }

        [WebMethod]
        public string About()
        {
            return "Version 1.0";
        }

        [WebMethod]
        // ResponseFormat is JSON
        [ScriptMethod(ResponseFormat=ResponseFormat.Json)]
        public string ListJSON()
        {
            ArrayList list = new ArrayList();
            _Contact = new Contact("First Name 1", "Last Name 1");
            list.Add(_Contact);

            _Contact = new Contact("First Name 2", "Last Name 2");
            list.Add(_Contact);

            Contact[] resultList = (Contact[]) list.ToArray(typeof(Contact));

            // JSON Response
            JavaScriptSerializer jss = new JavaScriptSerializer();
            string serializedObj = jss.Serialize(resultList);

            return serializedObj;
        }
    }

    ///
    /// our serializable contact class
    ///
    [System.Xml.Serialization.SoapType(Namespace="urn:XmlJsonContacts")]
    public class Contact
    {
        private string _firstName;
        private string _lastName;

        public Contact() {}

        public Contact(string firstName, string lastName){
            this._firstName = firstName;
            this._lastName = lastName;
        }

        public string firstName
        {
            get { return this._firstName;}
            set { this._firstName = value;}
        }

        public string lastName
        {
            get { return this._lastName;}
            set { this._lastName = value;}
        }
    }
}

Download the .Net source code.
[wpdm_file id=9]

Running ASPX Web Services on Mac OS X using Mono.

Create a Mono Develop Solution for the source code.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.