Quick tip: Saving custom types in IsolateStorageSettings

Its been a while since I used IsolatedStorageSettings from scratch.  I wrote a wrapper several months ago and its just worked since.  So when I was working on a side project recently (very recently) I was pulling my hair out trying to figure out why IsolatedStorageSettings didn’t appear to be working.  I could add an business entity to the dictionary and immediately retrieve it but after the app tombstoned or shutdown the dictionary was empty.  No errors were being thrown.  No data.

I tried clearing out the dictionary thinking there was a bad value in there from a corrupted state.  I searched and searched in the app for other places I might be effecting the dictionary but found none.  I placed the storage logic in three different places.  Nothing worked.

Finally, I binged around to see if anyone else was having a similar issue and found this post where Joel Johnson (MVP in Atlanta) answered this question for another developer.  You must mark the custom type with the DataContract attribute and then any properties you wish to serialize with the DataMember attribute.  Thanks Joel!

    [DataContract]
    public class Article
    {
        [DataMember]
        public string Title { get; set; }

        [DataMember]
        public string Url { get; set; }

        [DataMember]
        public string Description { get; set; }
    }

I searched around for this solution after the fact and it only seems to be buried within comments or forum posts.  The MSDN documentation for IsolatedStorageSettings doesn’t mention it at all.  So don’t assume (like I did) that the IsolatedStorageSettings dictionary can store custom types out of the box.  You must make them serializable first. 

Happing Coding

Leave a Reply