Storing something other than a string in SuspensionManager.SessionState

windows-8, windows-runtime

Solution

You will need to do two things:

Firstly, ensure the type you are (de)serializing is decorated with the `DataContract` attribute from `System.Runtime.Serialization` and ensure it's members are decorated appropriately. For example in c#:

[DataContract]
public struct Product
{
     [DataMember]
     public Guid Id { get; set; }

     [DataMember]
     public DateTime ManufactureDate { get; set; }

     [DataMember]
     public decimal Cost { get; set; }  
}

Secondly you will need to call SessionManager's `AddKnownType<T>()` (with T being the type you need) before attempting to (de)serialize it.

Problem

The sample apps include a Suspension Manager class that stores session state in a Dictionary, but never attempt to store anything except a string in it. Whenever I store anything else but a string, then trigger the serialization, I get the following error Type 'System.RuntimeType' with data contract name 'RuntimeType:http://schemas.datacontract.org/2004/07/System' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer. Do I have to do something else to be able to store other types in SessionState?

Original source