WCF is VERY VERY elegant!! If you’re unfamiliar with WCF, boy are you behind times! No really, I mean it! Stop whatever you’re doing and learn WCF. This is a technology that every .NET dev will be required to know. Please see What is WCF? for a basic introduction on WCF. Followed by writing a simple Hello World WCF service, a WCF Client, and hosting such a service!
Also, I had pioneered the work of properly making WCF work in SharePoint 2007. Well, the good news is SharePoint 2010 has wholly embraced WCF. I mean, in every single respect. SharePoint 2010 has WCF flowing through it’s veins!!! I mean, the service architecture, client object model, ADO.NET data services, REST API, heck every damn thing in SP2010 uses WCF. .. I told you .. shudda learnt it :-). Anyway, still not too late! And my book will cover it well enough too.
WCF is cool because it is very very versatile. It is very well architected. One of the things WCF comes with is very good support for very thin clients, i.e. JavaScript, JSON etc.
Well y’know, JSON is awesome for many reasons too. Why I like JSON?
- It’s lightweight, quick and easy to transfer
- It is damn easy to parse in JavaScript
- It serializes and deserializes without messing up your HTML or such text with escapable characters
- It works beautifully with jQuery
Now it is pretty damn easy to parse in JavaScript, but did you know that WCF has pretty good support for deserializing JSON as well? I mean, right in C#!
Assuming that your WCF service looked like this –>
1: [ServiceContract]
2: public interface IMyWCFContract
3: {
4: [OperationContract]
5: [WebGet(UriTemplate = "OperationName?parameterID={parameterID}", ResponseFormat = WebMessageFormat.Json)]
6: BusinessObjectType SomeCoolOperation(int parameterID);
7: }
You could deserialize it into a strongly typed .NET object as shown below:
1: HttpWebRequest webRequest = WebRequest.Create(serviceUrl) as HttpWebRequest;
2: HttpWebResponse webResponse = webRequest.GetResponse() as HttpWebResponse;
3: Encoding enc = Encoding.GetEncoding(1252);
4: StreamReader configStream = new StreamReader(webResponse.GetResponseStream(), enc);
5: string configuration = configStream.ReadToEnd();
6: JavaScriptSerializer jSerialize = new JavaScriptSerializer();
7: BusinessObjectType businessObject = jSerialize.Deserialize<BusinessObjectType>(configuration);
The above code will give you a strongly typed instance of BusinessObjectType in the client code!
Keep being cool!