WCF throttles properties without a "set". Any workaround?

I have a class that I pass as a result of a service method, and this class has a get-only property:

[DataContract] public class ErrorBase { [DataMember] public virtual string Message { get { return ""; } } } 

I get an exception from the service side:

System.Runtime.Serialization.InvalidDataContractException: The method for the "Message" property of type "MyNamespace.ErrorBase" has not been set.

I should only have this property as a getter, I cannot allow users to assign a value to it. Any workaround I could use? Or am I missing an additional attribute?

+85
c # properties wcf datacontractserializer
Feb 24 '10 at 2:29
source share
8 answers

Give a message to the public recipient, but a secure setter, so that only subclasses (and DataContractSerializer, because it is cheating :) can change the value.

+93
Feb 24 '10 at 2:34
source share

Even if you do not need to update the value, setter is used by the WCFSerializer to deserialize the object (and override the value).

This is what you need: WCF DataContracts

+12
Feb 24 '10 at 2:31
source share
 [DataMember(Name = "PropertyName")] public string PropertyName { get { return ""; } private set { } } 
+7
Aug 6 '12 at 11:21
source share

If you only have a recipient, why do you need to serialize the property at all. It looks like you can remove the DataMember attribute for a read-only property, and the serializer will simply ignore the property.

+5
Sep 23 '16 at 21:18
source share

Could you just configure do-nothing?

 [DataContract] public class ErrorBase { [DataMember] public virtual string Message { get { return ""; } set { } } } 

Or does the DataContract barf serializer also do the same?

+3
Feb 24 '10 at 6:05
source share

Properties with the DataMember attribute always require a set. You must rewrite the simmilar object to the client application, since DataContract members can always be assigned values.

+2
Feb 24 '10 at 6:16
source share

I had this problem with ASP.NET MVC, and I wanted to use the DataContractSerializer to be able to control element names in JSON output. In the end, I switched the serializer to JSON.NET, which supports properties without setters (without DataContractSerializer) and property name control (which the built-in JSON serializer in ASP.NET MVC does not) through [JsonProperty(PropertyName = "myName")] .

+2
Sep 13 2018-11-11T00:
source share

If this is a viable option, then instead of ErrorBase , define it as the base class as follows:

  public interface IError { string Message { [OperationContract] get; // leave unattributed set; } } 

Now, despite the fact that the setter exists, it is not available to the client through the WCF channel, so it is as if closed.

+2
May 25 '12 at 22:32
source share



All Articles