Thursday, September 18, 2014

Converting my Microsoft ASMX Web Services to WCF Services to Use with jQuery and iOS

I have been using standard Web Services (.ASMX) with Visual Studio for a few years now but I wanted to move to the new WCF services.  I like the idea behind them and how universal they are.  I also do not want to be in a predicament when Microsoft decides to no longer support those kind of services and only WCF services.

So, I was looking into a way that I can port these services over.  Most of these services are consumed either with jQuery or on iOS devices.

I am creating simple web services to retrieve data from the service or post some transactions back.  So, let me share how I setup my responses.  I have created some class objects to hold my data.

    public class jsonCustomComments
    {
        public string Comment_ID { get; set; }
        public string Comment_Title { get; set; }
        public string Comment_Text { get; set; }
    }
 I then create the function to retrieve this data:

        public List<jsonCustomComments> GetListOfUserCustomComments()
        {
            PT_COMMON ptc = new PT_COMMON(con);
            return ptc.GetListOfCustomComments(con._USERID);
        }
This will create a an array of jsonCustomComment Objects filled with the data I need to send.

Now, let us look at what I was doing with the Old Web Service (.ASMX) and how I was handling the request:

        [WebMethod(Description = "Returns a list of a Users Custom Comments.")]
        [System.Web.Services.Protocols.SoapHeader("responseSoapHeader")]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public void GetListOfUserCustomComments(string CONID)
        {
            JavaScriptSerializer js = new JavaScriptSerializer();
            PT_INSPECTION_SERVICES ws = new PT_INSPECTION_SERVICES(CONID, GetUserName(responseSoapHeader));

            Context.Response.Output.Write(js.Serialize(ws.GetListOfUserCustomComments()));
            Context.Response.ContentType = "application/json";
            Context.Response.Flush();
            Context.Response.End();
        }
To be able to get clean basic json I would use the JavaScriptSerializer() to Serialize the results of calling my function.  If I returned my results in json it was tagged with a bunch of junk, where all I wanted was clean json.  This will give me basic Key/Value pairs to work with in jQuery and Apple iOS.

So, now I tried to move this to the new WCF Services.  The problem was that returning my value as a json response, Microsoft again adds a lot of fluff that I don't want to my json.  I just want clean Key/Value pairs of information.

So, with my new Service setup and my contract created, this is the code I used:

        [OperationContract, WebGet(ResponseFormat=WebMessageFormat.Xml)]
        public string GetCustomCommentList(string CONID, string UserID)
        {
            WebOperationContext.Current.OutgoingResponse.ContentType = "text/html";
            PT_INSPECTION_SERVICES ws = new PT_INSPECTION_SERVICES(CONID, UserID);
            JavaScriptSerializer js = new JavaScriptSerializer();
            return js.Serialize(ws.GetListOfUserCustomComments());
        }

I needed to change the Message Format  to XML, then change the OutgoingResponse.ContentType to "text/html".  This will then give me the response with basic Key/Value Pairs.  Easy to convert to a Dictionary Object in iOS and easily parse the data in jQuery.