I'm struggling with tombstoning support in my WP 7.1 app. I have a single long-lived instance of DataServiceContext, which I serialize and deserialize using a DataServiceState helper. Upon deserialization, I can successfully retrieve an instance of my User class from the context using a URI as a lookup key. E.g.,
var context= ... a deserialized DataServiceContext ...
var userIdentity = ... a URI that identifies a User instance ...
User user = null;
context.TryGetEntity(userIdentity.ToString(), out user);
But if I try to load a property on the User instance (that I've just pulled from the context) as follows:
context.BeginLoadProperty(user, "Foo", MyCallback, null);
the context throws an InvalidOperationException with message "The context is not currently tracking the entity." It appears that deserialization does not preserve the referential integrity of the context.
I've created a simple app to demo this issue. Download from Skydrive: http://sdrv.ms/MhctLA
The app uses the sample data service at http://services.odata.org/OData/OData.svc. I manually ran dataSvcUtil to create the proxies in ODataDemo.cs. Note that I had to add the DataContract attribute on class DemoService in the generated code, but no other changes have been made.
Once the app is running, tap on the "data service context" button. The following page will load all of the Products from the data service, randomly select one to retrieve from the context, cache the identity URI of that product, and then load the Category property of the product.
Next, tap the "serialize" button. The app serializes the context using a DataServiceState.
Finally, tap the "deserialize" button. The app deserializes the context, and then attempts the following against the resurrected context:
- Get a product using DataServiceContext.TryGetEntity and the cached URI.
- Get the URI for that same product using DataServiceContext.TryGetUri.
- Load the Category property of the product.
Step 1 succeeds, while steps 2 and 3 fail. The fact that TryGetUri cannot find the descriptor for an entity that I just retrieved from the context indicates that something is wrong with the deserialized context.
Am I doing something dreadfully wrong? Or is this a bug?
I will have a look at this and get back to you as soon as possible. Thanks for sharing your repro project it'll be useful.
Edit: Looks like this is a bug. A workaround is to throw away the context instance and attach the instance to a new context.