Order Fixation

One of my current tasks for a client has been to facilitate the integration between a Java client and a WCF service. The mechanism for doing this is JSON and I have been quite grateful that WCF makes it relatively easy to implement this type of communication. However, there is one area that has been causing some grief for me (and my Java colleagues) that I finally create a workaround for yesterday.

The source of my problem starts with the contract used by the WCF service. A number of the methods include parameters that are abstract data types. Consider the following class declaration.

[KnownType(ButtonCommand)]
[KnownType(MouseCommand)]
[DataContract]

public abstract class AbstractCommand

The AbstractCommand class is just a base class for a couple of concrete classes (ButtonCommand and MouseCommand). The WCF method is declared as accepting an AbstractCommand type, but the value passed into the method will be either a ButtonCommand or a MouseCommand.

[OperationContract]
[WebInvoke(BodyStyle=WebMessageBodyStyle.Wrapped,
   RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
StatusCode ExecuteCommand(AbstractCommand command);

Naturally, when the client invokes this method, the expectation is that a concrete class instance will be passed along. However, if all you do is pass in the JSON for one of the concrete classes in, you get an exception saying that an abstract class cannot be instantiate. The reason for the error can be found in how WCF dispatches message. When the request arrives, WCF examines the message to determine which method is to be invoked. Once identified, the parameters for the method (found in the JSON) are created. This creation involves instantiating an object using the default parameterless constructor for the type and then assigning property values as found in the JSON. However, the AbstractCommand type cannot be instantiated (it is abstract, after all). And there is no immediately apparent mechanism to determine which concrete type should be used.

To address this final problem, Microsoft introduced the idea of a type hint. The JSON for a concrete type passed into the ExecuteCommand method would look something like the following:

{"command":{"__type":"ButtonCommand:#Command.Library", "prop1":"value", "prop2":"value"}}

The name/value pair of “__type” is used by WCF to determine the type that should be instantiated before performing the property assignment. This is conceptually similar to how types are provided through SOAP. This mechanism does have the hard requirement that the “__type” value be the first pair in the JSON list.

Enter Java into the picture. In order to communicate with my WCF service, the client using the Java JSONObject class. This class takes a collection of name/value pairs and converts it into a JSON-formatted array. This array is then sent to my method, where it goes through the previously described process. However the JSONObject seems to take the portion of the JSON specification where it says the name/value pairs are ‘unordered’ to heart. There is (apparently) no way to force a particular name/value pair to be emitted as the first pair. In other words, the JSONObject cannot reliably produce JSON for a concrete type that is being passed into a parameter of an abstract type.

Crap.

Well, it was actually stronger language than that once I figured this out.

The solution was not nearly as complicated as identifying the problem. I created a WCF endpoint behavior extension. In the AfterReceiveRequest event handler, I look at the body of the request (which has already been converted into XML, as it turns out..a surprise, albeit an easier choice format for the next step). If the “__type” property is the first pair in the JSON array, it is converted to a “__type” attribute in the XML element. Otherwise, it appears as a regular child node. So the transformation consists of finding the XML elements that have a name of “__type” and moving the value up into an attribute of the parent node. The modified message is then injected back into the WCF pipeline. And now the request is processed as desired.

TechEd Developer EMEA 2008

I found out a few days ago that I've been selected to speak at TechEd EMEA in Barcelona. I'm really excited as this is not only my first time speaking outside of North America, it's my first trip to continental Europe. My session is on using ASP.NET AJAX with SharePoint 2007.

Integrating ASP.NET AJAX with SharePoint.
SharePoint provides a great infrastructure for quickly building intranet and Internet applications. ASP.NET AJAX provides a foundation for creating highly productive Web interfaces. Combined they are two great tastes that taste great together! In this session we will cover the basics of working with ASP.NET AJAX inside of SharePoint 2007. We will take a look at how to prepare a Web Application for ASP.NET AJAX and how to use various ASP.NET AJAX tools such as the JavaScript libraries, JSON-enabled Web services and UpdatePanels to build add rich interactivity to your SharePoint sites.

If you're attending the conference and find the session interesting the please feel free to go to the session list and rate it. I don't know for sure but I think that your rating and the number of people who give a rating have an effect on the scheduling of the session. I'm positive that the number of ratings you receive helps determine if your session will repeat.

I'm not going to be able to attend the PDC this year (this event and the PDC were too close together) so I'm glad there's a PDC track at TechEd. From all reports PDC is going to rock so at least I'll be able to get a taste of the content.

I'll be Twittering (http://twitter.com/rob_windsor) and hopefully blogging from the conference. If you're there feel free to look me up or better yet drop by my session.

Real World AJAX with ASP.NET Session

I have to give credit to Nikhil Kothari. He just ran a session the way that I like to see sessions run. As I read the title and abstract for the session, I was expecting not much in the way of new information. I actually attended the session mostly because it was the only one in this time slot that might be of interest.

Nikhil started out with just some basic stuff on AJAX. But very quickly he got past that and the session became very interesting. The idea is that if you plan on using AJAX in a "real" application, there are a number of issues that need to be address. Simple issues to identify, but complex to deal with. For example:

History

Say you have a page that makes a number of AJAX calls, updating the screen each time. Then you click on the back button. The typical action is to go to the page prior to the initial view of the current page. But what you generally what to do is go to the results from the previous AJAX call.

The solution is to use an AJAX UpdateHistory control. More details can be found on Nikhil's weblog here. It requires some work by the developer to determine when the browser history needs to be updated, but if you put the effort in, your users will love you. OK, not really. This is one of those features that users will ignore if it's there, because your site is working as they think it should. But if you don't have it, they will hate you. It's almost the same thing.

Indexability

If you have a page that uses AJAX to retrieve and display information, you have a problem with making that page appear in the appropriate place within Google. After all, Google depends on static data for its indexing.

The pattern that Nikhil is suggesting is to create a static page that contains all of the data. Make the data static on page. Put the data into a hidden div. Then create a separate frame and use script to pull the data from the hidden div into the frame. When viewed normally, the data will appear in the frame. When crawled by a spider, the script is not executed, so the data is just available for indexing. This pattern might not work in all situations, but there are enough to make it worthwhile being aware of. Again, a blog post (here) provides some additional details.

Overall, a nice presentation with some useful takeaways.

AJAX "Get Started" videos

For anyone trying to get started with AJAX and the new AJAX control toolkit here are a couple of short videos that you could watch while sipping on your coffee :)

ASP.NET AJAX Extensions: Installation and Setup

ASP.NET AJAX Control Toolkit: Installation and getting started

You may also want to bookmark http://ajax.asp.net.