WCF DataContract with readonly properties

Posted by Asaf R on Stack Overflow See other posts from Stack Overflow or by Asaf R
Published on 2010-03-22T18:54:50Z Indexed on 2014/08/24 22:21 UTC
Read the original article Hit count: 303

Filed under:
|
|
|

I'm trying to return a complex type from a service method in WCF. I'm using C# and .NET 4. This complex type is meant to be invariant (the same way .net strings are). Furthermore, the service only returns it and never recieves it as an argument.

If I try to define only getters on properties I get a run time error. I guess this is because no setters causes serialization to fail. Still, I think this type should be invariant.

Example:

[DataContract]
class A 
{
   [DataMember]
   int ReadOnlyProperty {get; private set;}
}

The service fails to load due to a problem with serialization.

Is there a way to make readonly properties on a WCF DataContract? Perhaps by replacing the serializer? If so, how? If not, what would you suggest for this problem?

Thanks,
Asaf

© Stack Overflow or respective owner

Related posts about .NET

Related posts about wcf