WCF chokes on properties with no “set ”. Any workaround?

后端 未结 9 747
别跟我提以往
别跟我提以往 2020-12-02 11:34

I have some class that I\'m passing as a result of a service method, and that class has a get-only property:

[DataContract]
public class ErrorBase
{
  [DataM         


        
相关标签:
9条回答
  • 2020-12-02 12:24
    [DataMember(Name = "PropertyName")]
    public string PropertyName
    {
        get
        {
            return "";
        }
        private set
        { }
    }
    
    0 讨论(0)
  • 2020-12-02 12:26

    If you only have a getter, why do you need to serialize the property at all. It seems like you could remove the DataMember attribute for the read-only property, and the serializer would just ignore the property.

    0 讨论(0)
  • 2020-12-02 12:32

    I had this problem with ASP.NET MVC and me wanting to use DataContractSerializer in order to be able to control the names on the items in the JSON output. Eventually I switched serializer to JSON.NET, which supports properties without setters (which DataContractSerializer doesn't) and property name control (which the built-in JSON serializer in ASP.NET MVC doesn't) via [JsonProperty(PropertyName = "myName")].

    0 讨论(0)
提交回复
热议问题