Consuming WCF WebService from jQuery AJAX (with JSON)

So the guys at work are pretty sold on jQuery now.  Everybody who've used it don't want to go back to vanilla JavaScript (just as I predicted).

One of the things always sitting at the back of my mind is to bridge the call from jQuery.ajax to WCF WebServices.

Finally we had a chance to do this, and it turns out this was pretty easy.  Special mention to Alvin Shen and Ron Maman who checked this out.

 

  1. Create an AJAX-enabled WCF WebService
    image
  2. This will update your web.config - the only difference is the WCF bindings used - here are the two interesting bits

    <service name="SSW.WCF.Services.BookService">
       <endpoint address="" behaviorConfiguration="SSW.WCF.Services.BookServiceAspNetAjaxEndPointBehavior"
        binding="webHttpBinding" contract="SSW.WCF.Services.BookService" />
    </service>

    <behaviors>
       <endpointBehaviors>
          <behavior name="SSW.WCF.Services.BookServiceAspNetAjaxEndPointBehavior">
             <enableWebScript />
          </behavior>
       </endpointBehaviors>
    </behavior>

  3. Here's the jQuery AJAX call

    $(document).ready(function() {
        $.ajax({
            type: "POST",
            url: http://localhost/<web app name>/BookingService.svc/DoWork,
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(data) {
                alert(data.d);

            }
        });
    });

 

Incredibly easy, the hardest part was working what the url was going to be

 

UPDATE technologies list:

  • jQuery
  • WCF WebService (AJAX-enabled WCF WebServices are only available in ASP.NET 3.5 - where they added the webHttpBinding, which does JSON serialization)

UPDATE:
Note that ASP.NET does date serialization a bit differently - there is no proper JSON date standard, and the JavaScript date doesn't match the .NET DateTime class with timezone, you may need to do a bit of work to serialize back to a JavaScript date. 

 

Here's a fun service that we use for testing.

// Add more operations here and mark them with [OperationContract]
[OperationContract]
public object[] TestJSON()
{
    return new object[] { DateTime.Now, 1, 1.034f, null, "test" };
}

Seriously thinking to split this blog

I like blogging about what I'm doing, but in some aspect, there are different areas that I would like to blog about:

Software Development

  • Some really interesting problems and how we solve them
  • Some really annoying bugs and how we got around them
  • Some totally unexplained and undocumented feature and how we figured out how it all worked

Software Project Management

  • This is not my forte, but as you get experienced in this industry it's probably inevitable that you end up doing project management work
  • Project management deals a lot with the risks, processes and communication with clients.

Gaming

  • What I thought about WOW
  • What I'm thinking about WAR
  • XBox, XBox Live
  • PC gaming, in general

Rants

  • Stuff that happens, usually something about my laptop - honestly if my laptop was working fine you guys would never even know I was using a Dell Studio 17 (BTW, after the technician fixed it, it's working really well now)

Code Archaeology

Define: Archaeology

A discipline involving the study of the human past through its material remains.

This is probably not an uncommon scenario:

  • You have an outdated project (or project plan)
  • You have some source code (may be in different branches in your source control)
  • You have some old emails about what various people needed
  • For some reason (low priority, budget, time), things were never done
  • You have a list of players who have left the project / company / industry / country

Your task, is to resurrect the project and make it work (again):

I was looking for a word to describe this special branch of Computer Science discipline.  And I think I've found it. 

Archaeology.

In my words, a study of the remains of ideas that were left in the system, and trying to reconstruct the past to work out a picture for the present and the future.

iPhone special: No contacts in the contact list when I couldn't talk to my Microsoft Exchange server

I interrupt my usual rant about .NET (and Dell laptops) to bring you a rant about my iPhone.

Story begins like this...

So we had a bit of a DNS hiccup with our Microsoft Exchange server.

When I pop over to use my iPhone, it says "Cannot Get Mail. The connection to the server <mail server> failed"

Fair enough.  Not the iPhone's fault.

Then I lose all my contacts.  In my contacts list, there was "No contacts".  There were 200 contacts before.

Panic

What, really?  Yes.

I must crazy.  Someone who designed this must be crazy.  It couldn't be the iPhone.  It must have been me.  I thought I must be mad.  It must have been a bad push / sync, or something.

Resolution

Anyway, I frantically tried to look for a solution, but it was only until when I finally got it to connect to Exchange server again after the DNS issues were sorted out, and hey, all my contacts re-appeared again.

I contemplated on restoring the phone, but my contacts were synchronized with Exchange, iTunes isn't tracking it:

"Your contacts are being synced with your iPhone over the air from Microsoft Exchange."

So I couldn't sync mail for an hour.  I also have zero contacts in my iPhone for an hour.  From the point where the iPhone decided that it couldn't sync with Exchange, apparently it also decided that it should now hide (or remove - I can't tell) all my contacts.

I don't know whether to laugh or to cry.

jliu