Bulb Flash: Debugging DB entity creation in Silverlight!

Your silverlight client speaks to a WCF service which in turn uses some complicated logic (say LINQ to SQL) to interact with the DB.

You create an instance of the DB object on the client side(as per User’s inputs) and send the object to the service for saving.

You are greeted with a


Your object as huge and its impossible to recreate it in code , put in a windows client and debug into the webservice!!

Common scenario?? Now how do you make out what went wrong?

If you’ve faced this issue and sure you know its near impossible to debug into the WCF service from the silverlight client

Here is what i did.

  1. I implemented DataContract Serialization to save the object i want to save into an XML string.
  2. I run the application. Put a breakpoint at LoadData and save the XML string into a notepad..

using System.IO;
using System.Runtime.Serialization;
using System.IO.IsolatedStorage;

///Store the object definition in an XML String

public void StoreObject() {

using(IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
    string[] names = isf.GetFileNames("*.xml");
    if (names.Length != 0)
        foreach (string name in names)
    IsolatedStorageFileStream isfs = new IsolatedStorageFileStream("objectInfo.xml", FileMode.CreateNew, isf);

    StreamWriter sw = new StreamWriter(isfs);

    DataContractSerializer ser =
        new DataContractSerializer(typeof(SampleObject));
    ser.WriteObject(isfs, _currentSampleObject);
string s = LoadData("objectInfo.xml");


///Load the stored data into an XML string

private string LoadData(string fileName)
         string data;
         using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())

             XDocument doc = XDocument.Load(new IsolatedStorageFileStream(fileName, FileMode.Open, isf));
             data = doc.ToString();

         } return data;

3. I then create a windows project and add the WCF service as the usual service reference.

4. In this windows project i add an XML file  and paste the xml string saved above into it.

5. I load the XML document in some button click and send it to the webservice.

XDocument doc =

6. In the Webservice i deserialize the string to get back the object i saved in step 1 and call my save function

private static void SaveObjectFromString(string objectDefinition)
           DataContractSerializer ser = new DataContractSerializer(typeof(SampleObject));

           byte[] byteArray = new byte[objectDefinition.Length];
           ASCIIEncoding encoding = new
           byteArray = encoding.GetBytes(objectDefinition);

           // Load the memory stream
           MemoryStream memoryStream = new MemoryStream(byteArray);

           XmlDictionaryReader reader =
             XmlDictionaryReader.CreateTextReader(memoryStream, new XmlDictionaryReaderQuotas());

           SampleObject info =
               (SampleObject )ser.ReadObject(reader, true);

///save the created object. Same method would be getting called from the silverlight client also


7. Now i set the windows project as the start up project and do an f11 to debug into the webservice…

Now i know what was wrong with the object i was trying to save from my silverlight client!!

In short…

1. Use the DataContractSerializer to serialize and store your object in the isolated storage

2. Read the object into a string

3. Load it into an XML file in a windows application

4. Call the Webservice with your serialized object

5. Deserialize the object in your webservice and debug to see what the issue was!!!

Hope this Bulb flash saves you some time and a couple of grey cells!!