Reputation: 10672
As the error shows I don't have a setter for my property, but I don't want a setter, it should be readonly.
Upvotes: 12
Views: 11411
Reputation: 49974
Edited: Make the setter internal
.
This will still be settable within the assembly, but it is a nice trick that works well when used on data objects located within an assembly that is consumed by others, as those consuming assemblies will not be able to set the property, but the various serializers can.
Upvotes: 17
Reputation: 34198
Your question's a bit vague, but I guess this is the answer you're looking for:
Default Serialization will only work for read-write properties, because you can't rehydrate an object without setting property values. If you want it to work with the property being readonly, you need to implement the serialization interface yourself, rather than just adding attributes.
Assuming you're using DataContract serialization, I think the best option is to implement ISerializable and implement the methods yourself.
Upvotes: 3
Reputation: 1038780
Remember that WCF needs to create an instance of the object from its serialized representation (often XML) and if the property has no setter it cannot assign a value. Objects are not transferred between the client and the server but only serialized representations, so the object needs to be reconstructed at each end.
Upvotes: 4