Gotcha with Compact Framework 3.5

My current project has me with many hats, among which is a developer for the mobile handheld system that our client will use to talk to the central architecture via a WCF layer.  This isnt full blown SOA, mainly because you can think of the handheld as complimentary to the web piece that was created first.  There isnt enough overlap to justify having a full blown SOA layer.  The idea was presented but we choose to take this approach.

Now, one of the things I have come to know about the Compact Framework is that it is totally useless and terrible, except when compared to every other phone/mobile device OS I have ever worked with. There is a long list of things that are not supported from the original framework, such as the TryParse methods.  And of course, the way you interact with WCF is also heavily restricted.  So much so, you must use a special tool to generate the proxy classes, but that isnt the gotcha, to explain the gotcha, consider the following class:

   1: [DataContract]

   2: public class Person

   3: {

   4:     [DataMember]

   5:     public string FirstName { get; set; }


   7:     [DataMember]

   8:     public string LastName { get; set; }


  10:     [DataMember]

  11:     public int Age { get; set; }

  12: }

What you would end up with on the client via the generated proxy class is essentially a class that looks like this:

   1: public class Person

   2: {

   3:     public string FirstName { get; set; }

   4:     public string LastName { get; set; }

   5:     public int Age { get; set; }


   7:     // new

   8:     public bool AgeSpecified { get; set; }

   9: }

So, as you can see for non-string properties a “Specified” field is added.  This tells the Compact Framework generated base class whether to serialize the data from this property or not.  I think this is NOT a good idea.  As evidenced by fighting with the system for hours trying to understand why things weren’t serializing right.  This just clicked for me today.  To me, it speaks to pure laziness by Microsoft.  We have patterns, such as Observer and PropertyChanged, for handling this very scenario.

So the trick here is when you are passing your data pack to the server in an object, you need to set the “specified” property to true. To actually see the value on the server.  Hope this helps people, cause it drove me nuts.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s