The execution of the InstancePersistenceCommand SaveWorkflow was interrupted by an error...get-only collection is not supported with NetDataContractSerializer
-
Wednesday, April 04, 2012 8:44 AM
Hi all,
I have a workflow service that receives as input a DataContract of type SomeOtherClass:
[DataContract] public class SomeClass { [DataMember] public Guid Id { get; set; } [DataMember] public string Data { get; set; } } [DataContract] public class SomeOtherClass { private Collection<SomeClass> _someClassCollection; [DataMember] public Guid Id { get; set; } [DataMember] public Collection<SomeClass> SomeClassCollection { get { if (_someClassCollection == null) _someClassCollection = new Collection<SomeClass>(); return _someClassCollection; } } }
Everything works fine, except that when the workflow instance is persisted I get the following error in AppFabric:
The execution of the InstancePersistenceCommand named {urn:schemas-microsoft-com:System.Activities.Persistence/command}SaveWorkflow was interrupted by an error. InnerException Message: The use of type 'System.Collections.ObjectModel.Collection`1[[MyAssembly.SomeClass, MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=...]]' ' as a get-only collection is not supported with NetDataContractSerializer. Consider marking the type with the CollectionDataContractAttribute attribute or the SerializableAttribute attribute or adding a setter to the property.
I understand that creating a new class that inherits from Collection<SomeClass> and decorating it with [CollectionDataContract] solves this problem, however I don't understand totally why, and I don't want to apply a setter to the property. It seems that workflow foundation under the hood is using the NetDataContractSerializer but how and why does this not work since it works with the DataContractSerializer?
All Replies
-
Friday, April 06, 2012 9:41 PM
The NetDataContractSerizlizer includes type information where the DataContractSerializer does not. This information is required to properly serialize/deserialize in the absence of WSDL.
Sr. Program Manager, Windows Azure Platform Team(WF) http://blogs.msdn.com/rjacobs http://www.twitter.com/ronljacobs
- Marked As Answer by pacojones Tuesday, June 26, 2012 9:11 AM
-
Monday, April 09, 2012 11:09 AM
Hi Ron thank you for your feedback.
This means that under the hood, Workflow Services (xamlx) use somehow the NetDataContractSerializer to serialize/deserialize the mapped variables when persisting?
I'm curious because if I change the above code to this it works:
[DataContract] public class SomeClass { [DataMember] public Guid Id { get; set; } [DataMember] public string Data { get; set; } } [DataContract] public class SomeOtherClass { private SomeClassCollection _someClassCollection; [DataMember] public Guid Id { get; set; } [DataMember] public SomeClassCollection SomeClassCollection { get { if (_someClassCollection == null) _someClassCollection = new SomeClassCollection(); return _someClassCollection; } } } [CollectionDataContract] public class SomeClassCollection: Collection<SomeClass> { }
So the NetDataContractSerializer does not know by itself how to serialize/deserialize concrete type collections if they only have get accessor, except if you create your own class that inherits from that concrete type collection and mark it as CollectionDataContract?
Thank you again!

