Nairooz Nilafdeen's Blog
Monday, January 23, 2012
WCF REST versioning, JSON and Dynamic objects
WCF makes things very easy, if the client sends a JSON formatted string, WCF infrastructure will de-serialize this into a custom type that you have specified in your operation contract.
For an example...
[WebInvoke(Method="POST"....)]
public void Save(Student student){}
so if you send a JSON string like this on your POST body
{Name : "Nairooz Nilafdeen", Age : 26}
...the WCF infrastructure will de-serialize the JSON string into the Student object (as long as the JSON key-value matches the properties in the Student type)..
Now, the problem with this approach is when you send a JSON string that contains an additional property that is not available in the Student class, now WCF will not be able to de-serialize as the JSON string does no exactly match with the Student type.
In a few situation you want to keep one simplified API that users would want to access, but how do you take versioning into account?
The simplest way would be to except a stream into your operation, so now it becomes like this...
[WebInvoke(Method="POST"....)]
public void Save(Stream stream){}
(you can alternativley make 2 URLs available for differen versions...)
Now all the client needs to do is send any version of the Student type but also send the version of the service it's using in the header..on the service side you would look at the header and according to the version you will fill in the correct version of the Student type...
but, now the issue is that you are getting a JSON stream, now you need to read this and get the correct values for the stream...
Let .Net dynamic do the talking, you can use the DynamicJson library available at codeplex (google it...)and you can do this...
dynamic jsonObject = DynamicJson.Parse(stream);
you can access the Name property of the JSOn stream like this...
string name = jsonObject.Name;
and whats more interesting is when you are creating a unit test to test out this API...
simply create a ExpandoObject (another dynamic object) and just keep on filling what you need, no need to create a static class...
ExpandoObject obj = new ExpandoObject();
obj.Name = "Nairooz Nilafdeen";
I know the post is not really concise, but noting it down for my future use.
Sunday, January 22, 2012
Being a good Leader
Thursday, October 6, 2011
LINQ to Object JOIN
I was reviewing a piece of LINQ to Object Join query of one of my colleagues and a code that is written like this (example, not the exact code, the actual query was complex then this…)...
(from i in col1 join j in col2 on i equals j select i).ToList();
where i == j select i).ToList();
my Colleague, argued that Microsoft would have implemented the JOIN operator in a better way then a 2 loops running on each other…
I ran a few test on the 2 queries above, and the query that used the JOIN operator was way faster…
The reason is that the JOIN operator (the extension method) is implemented such that the outer collection’s key is enumerated once and put into a hash table, next the inner collection is iterated and it’s key is checked to see if it’s there in the hashtable, this is just O(1) operation, this is way faster then just comparing a variable in an outer loop with something in an inner loop.
Thank you Rajiv…
Wednesday, May 18, 2011
.Net Reflector to Telerik JustDecompile

Tuesday, May 17, 2011
Entity Framework and POCO - No Support for Xml or Enums
Sunday, May 1, 2011
WCF Configuration Nightmare, MSBuild, IIS AppCmd
Friday, April 29, 2011
Moving Session Data from InProc to State Server or Database And There Problems
So in this post I would like to talk about ASP.NET sessions.
Ok...now we all know about ASP.Net sessions, we all know there are few methods which you can use to store session information, one common way and default way is store session state in the memory of the machine in which ASP.NET is running, the other way is to use the ASP.Net state server or to use the default SQL Server provider to store session data, we are not going back to the basics however, we should be careful in choosing where the session data will be stored and what sort of data will be stored.
It's particularly important to know that moving session data from one mechanism to another (in proc to state server)is not just a matter of changing the configuration file.
When using in-proc data is stored in memory, but when storing it in the state server or on a database like SQ Server, these session data needs to be serialized.
Not all data is serializable, like for an example the generic Dictonary or XElement class, so you have to be careful of what you store in the session, if your thinking that in the future you might go for a State server.
It depends on what data is stored in the session, for an example, the best thing for you to do is to store very less data in the session so that you don't use up a lot of server memory for storing user data.
There is also sometimes when you would want to store view State data in the session, hence, the amount of data that goes back forth from the user browser to the server is minimized, here is an MSDN Link on how you can do that.