DataContractSerializer Read-Only Property Serialization Error
I recently encountered an exception with a message that looked something like this:
There was an error deserializing the object of type Foo. No set method for property ‘’ in type ‘’.
This was coming out of WriteObject (or a similar method) of DataContractSerializer (or DataContractJsonSerializer, or some other flavor).
If you see this error, don’t be thrown off by the bizarre error message. If my experience holds true this error is in fact caused by a read-only property. Unfortunately, the error message won’t give you any clues to what the property is, and the offending type is probably NOT the type Foo, as the error message suggests.
Description of my scenario:
- Attempt to serialize an object with a property that is a list of WCF serializable objects (base type).
- The last item in the list is a sub type, “Foo”, that contains a property WITHOUT a setter. Serialization is ok.
- After the item of type “Foo”, another item of type “Bar” (a different sub type) is added. This type does NOT have any properties without a setter. Attempting to serialize the parent object now fails with message “There was an error deserializing the object of type Bar, My Assembly. No set method for property ‘’ in type ‘’.”
See this ticket and this stackoverflow post (author describes a slightly different scenario) for more information.
Good luck! :)