.

The constructor to deserialize an object of type ??? was not found

See solution to my problem below – I got the following stack trace (error) while binary deserializing a class:

The constructor to deserialize an object of type ‘Microsoft.Skype.Horizon.Business.DataCollection.Exceptions.DataCollectionException’ was not found.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Runtime.Serialization.SerializationException: The constructor to deserialize an object of type ‘Microsoft.Skype.Horizon.Business.DataCollection.Exceptions.DataCollectionException’ was not found.

Source Error:
return formatter.Deserialize(stream);

My code was:

[Serializable]
public class DataCollectionException : Exception
{
}
Solution:  simply add the following constructor

public DataCollectionException(SerializationInfo info, StreamingContext context)
        : base(info, context) { }

so that the code now looks like this:

[Serializable]
public class DataCollectionException : Exception
{
    public DataCollectionException(SerializationInfo info, StreamingContext context)
        : base(info, context)
    {
    }
}

Dictionary Serializer

The Dictionary class itself is not serializable, so we create a class SerializableDictionary and derive from Dictionary as well as the interface IXmlSerializable
in order to make a Serializable Dictionary.

[XmlRoot("dictionary")]
public class SerializableDictionary<TKey, TValue> : Dictionary<TKey, TValue>, IXmlSerializable
{
    #region IXmlSerializable Members

    public System.Xml.Schema.XmlSchema GetSchema()
    {
        return null;
    }

    public void ReadXml(System.Xml.XmlReader reader)
    {
        XmlSerializer keySerializer = new XmlSerializer(typeof (TKey));
        XmlSerializer valueSerializer = new XmlSerializer(typeof (TValue));
        bool wasEmpty = reader.IsEmptyElement;
        reader.Read();
        if (wasEmpty) return;
        while (reader.NodeType != System.Xml.XmlNodeType.EndElement)
        {
            reader.ReadStartElement("item");
            reader.ReadStartElement("key");
            TKey key = (TKey) keySerializer.Deserialize(reader);
            reader.ReadEndElement();
            reader.ReadStartElement("value");
            TValue value = (TValue) valueSerializer.Deserialize(reader);
            reader.ReadEndElement();
            this.Add(key, value);
            reader.ReadEndElement();
            reader.MoveToContent();
        }
        reader.ReadEndElement();
    }

    public void WriteXml(System.Xml.XmlWriter writer)
    {
        XmlSerializer keySerializer = new XmlSerializer(typeof (TKey));
        XmlSerializer valueSerializer = new XmlSerializer(typeof (TValue));
        foreach (TKey key in this.Keys)
        {
            writer.WriteStartElement("item");
            writer.WriteStartElement("key");
            keySerializer.Serialize(writer, key);
            writer.WriteEndElement();
            writer.WriteStartElement("value");
            TValue value = this[key];
            valueSerializer.Serialize(writer, value);
            writer.WriteEndElement();
            writer.WriteEndElement();
        }
    }

    #endregion
}

JSON serialization

//This is just used to serialize the results to the file as JSON and read it back
private static string ToJson(T obj)
{
MemoryStream stream = new MemoryStream();

try
{
DataContractJsonSerializer jsSerializer = new DataContractJsonSerializer(typeof (T));
jsSerializer.WriteObject(stream, obj);

return Encoding.UTF8.GetString(stream.ToArray());
}
finally
{
stream.Close();
stream.Dispose();
}
}

private static T FromJson(string input)
{
MemoryStream stream = new MemoryStream();

try
{
DataContractJsonSerializer jsSerializer = new DataContractJsonSerializer(typeof (T));
stream = new MemoryStream(Encoding.UTF8.GetBytes(input));
T obj = (T) jsSerializer.ReadObject(stream);

return obj;
}
finally
{
stream.Close();
stream.Dispose();
}
}