Question

I've been experiencing this on my phone when I try and tombstone my application.

Type 'System.Text.UTF8Encoding' with data contract name 'UTF8Encoding:http://schemas.datacontract.org/2004/07/System.Text' is not expected. 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.

I'm not using WCF or data contracts anywhere in my app, and I'm not executing any code on tombstone, so I'm not sure where this is coming from. I don't have any of that text or schema anywhere in my code either.

In the stack trace, I don't see any code of mine being executed.

System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeAndVerifyType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, Boolean verifyKnownType, RuntimeTypeHandle declaredTypeHandle)
...

at System.Runtime.Serialization.XmlObjectSerializer.WriteObjectHandleExceptions(XmlWriterDelegator writer, Object graph)

at System.Runtime.Serialization.XmlObjectSerializer.WriteObject(XmlDictionaryWriter writer, Object graph)

at System.Runtime.Serialization.XmlObjectSerializer.WriteObject(Stream stream, Object graph)

at Microsoft.Phone.Shell.StreamPersister.Serialize(IDictionary`2 dictionary, IEnumerable`1 knownTypes)

at Microsoft.Phone.Shell.StreamPersister.Save(ShellPageManager shellPageManager, String key, IDictionary`2 dictionary, IEnumerable`1 knownTypes)

at Microsoft.Phone.Shell.PhoneApplicationService.FireDeactivated()

at Microsoft.Phone.Execution.NativeEmInterop.FireOnPause()

How do I debug something like this?

Solution: Not easy, but enabling the exception in the debug menu is a start, then using shift+f9 to try out some values to see what could be causing it. The final answer is to clear out the state before the app closes with something like this.

Application_Deactivated or Closing(){
            SerializeToPhone(); //save state to phone manually
            var appService = PhoneApplicationService.Current;
            appService.State.Clear();
}

This way, you clear out the objects so the phone doesn't try to manage state for you. I've added my own code to save state to the phone using custom serializers and read from the phone on reactivate. I think if your app is simple enough, just some strings and numbers with no web security or facebook logins etc, you can leave it up to the phone to handle tombstoning. In my case I needed a more robust system.

Was it helpful?

Solution

It sounds like you've got an Encoding (or UTF8Encoding) variable in one of your types which is being automatically serialized so that your app can be restored. Have a look through your code and see if there are any variables like that anywhere. If there are, the next step is to work out whether you really need those variables.

OTHER TIPS

ApplicationState is serialized with a DataContractSerializer when tombstoned. It sounds like you're adding something which can't be desrialized correctly.
There are 2 alternatives to this:

  1. Ensure that whatever you're saving can be deserialized by the DataContractSerializer correctly.
    or
  2. Handle the serialization and deserialization yourself (with something faster than the DataContractSerializer, such as json.net) and only add the serialized strings to the state object.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top