Thursday, March 10, 2011

WCF DataContract serialization and deserialization in Silverlight

Here is how you can do serialization and deserialization of WCF DataContracts in Silverlight. This is needed when you use MessageContracts in WCF by wrapping DataContracts as XmlElement.

Serialization code:

DataContractSerializer serialize = new DataContractSerializer(request.GetType());
MemoryStream stream = new MemoryStream();
serializer.WriteObject(stream,request);
string res = Encoding.UTF8.GetString(stream.ToArray(),0,stream.ToArray().GetLength(0));
TextReader reader = new StringReader(res);
XDocument doc = XDocument.Load(reader);
XElement elt = (XElement)doc.FirstNode;

you can pass the elt in your MessageContract and send to the WCF service.

Deserialization code:

Once you receive the response from WCF service as XElement, this is how you can deserialize that to object.

XmlDictionaryReader reader = XmlDictionaryReader.CreateDictionaryReader(elt.CreateReader());
DataContractSerializer serialize = new DataContractSerializer(reader);
result - serialize.ReadObject(reader);

Wednesday, March 9, 2011

Einstein's Fish Puzzle

Recently, my wife got impressed with the Einstein's quote of "Everybody is a genius. But if you judge a fish by its ability to climb a tree, it will live its whole life believing that it is stupid" and asked me to solve his famous "Fish Puzzle". I managed to solve that puzzle in 25 minutes.

The puzzle goes like this;

1. In a street there are five houses, painted five different colours
2. In each house lives a person of different nationality
3. These five homeowners each drink a different kind of beverage, smoke different brand of cigar and keep a different pets


THE QUESTION: WHO OWNS THE FISH?

HINTS
1. The Brit lives in a red house.
2. The Swede keeps dogs as pets.
3. The Dane drinks tea.
4. The Green house is next to, and on the left of the White house.
5. The owner of the Green house drinks coffee.
6. The person who smokes Pall Mall rears birds.
7. The owner of the Yellow house smokes Dunhill.
8. The man living in the centre house drinks milk.
9. The Norwegian lives in the first house.
10. The man who smokes Blends lives next to the one who keeps cats.
11. The man who keeps horses lives next to the man who smokes Dunhill.
12. The man who smokes Blue Master drinks beer.
13. The German smokes Prince.
14. The Norwegian lives next to the blue house.
15. The man who smokes Blends has a neighbour who drinks water.

Answer is: German


Anyone can solve this puzzle. Imagine this is the zig zag puzzle which we used to solve. Replicate the hint scenarios similar to that. Then it will be easy to solve this puzzle. How I did, I used Excel 2003/2007 to solve this puzzle. (Some brainiac can do in their mind itself like my wife.. :) ). I jot down the hints in the excels and painted them in some colors to identify. So that, while re-shuffling we can ensure that we are not breaking the rules of the hints!!!

Voila!! Try out at your home and you can also become next Einstein!!! LOL :P

Some simple steps to convert ASP.NET application to Silverlight application

Sometime you may be asked to look at the possiblity of converting the ASP.NET application to Silverlight application. Trust me, there are no hard and fast rules to migrating ASP.NET to Silverlight due to various reasons;
  1. Silverlight runs in client-side; ASP.NET runs in the server-side. This means, you will not be able to use as much as functionalities available from server-side.
  2. Application Life Cycle is entirely different for Silverlight and ASP.NET. You need to familiarize yourselves first before starting to work on silverlight.

But, somehow I managed to find some steps which will ease the ASP.NET to Silverlight migration;
  1. Find the equivalent controls for ASP.NET controls in Silverlight
  2. Convert the .aspx pages to .xaml files
  3. Define the Page navigation in App.xaml.cs file (bit difficult to do)
  4. Silverlight support very few assembly references. Migrate the .aspx.cs code .xaml.cs code accordingly.
  5. If you encounter some assembly references breaking, convert those functionalities to WCF service. Good thing about silverlight is that, it supports communication with WCF service with some limitations (no WS-Security)
  6. Silverlight follows MVVM pattern, if you application built around MVVM or can be easily upgraded to MVVM pattern, you can migrate your ASP.NET application to Silverlight application at ease...

I will update this post, If I manage to find some other tweaks and tricks in converting ASP.NET to Siliverlight.