tags:

views:

283

answers:

4

Hi,

As the error shows I dnt have setter for my property, but I dnt want setter its just a readonly..any Ideas??

Will really appreciate it

Thanks

A: 

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.

slugster
Protected won't allow the property to be set from outside the class, so deserialization will still fail...
Dan Puzey
Thanks Slugster!!!For your reply...
BreakHead
@Dan - check my edit. I knew i had encountered this one before, but i had to go back and check how i dealt with it. You are right - i initially tried *protected* but found it didn't work, so used *internal* instead.
slugster
Thanks guys!!!I choosed a different way:set{Throw new Exception("You can't set value for this property.")}Thanksss for your Inputs Dan and Slugster
BreakHead
@slugster - I'm still surprised that internal works, from a code point of view (because the serializer still shouldn't be able to see that member from outside of the assembly!), but it's good to know that it does!
Dan Puzey
@Qutbuddin: you should test your method, because I suspect you'll find that the data in that property (and possibly in that class) won't make it across the wire, because deserializing will throw your exception.
Dan Puzey
A: 

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.

Darin Dimitrov
+1  A: 

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.

Dan Puzey
A: 

I choosed a different way: set{Throw new Exception("You can't set value for this property.")} Thanksss for your Inputs Dan and Slugster

BreakHead
@Qutbuddin: Reposting my earlier comment in case you missed it: you should test your method, because I suspect you'll find that the data in that property (and possibly in that class) won't make it across the wire, because deserializing will throw your exception.
Dan Puzey
Yeah, you are better off just having an empty setter instead of throwing an exception. Although a read-only property shouldn't need to be part of the serialization - it will still be available to you on the client side object, but doesn't persist any data itself so doesn't need to be serializable.
slugster