Search code examples
c#wcfserializationpropertiesdatacontract

What's wrong with Property without set?


I have a class used with a WCF service.

Interface defined

[WebGet(ResponseFormat = WebMessageFormat.Json,
        UriTemplate = "GetMyStuff/?p={param}",
        BodyStyle = WebMessageBodyStyle.Bare)]
MyResponseObject MyMethod(string param);

Among it's properties I had

public bool IsDecorated {
  get {
    return !String.IsNullOrEmpty(Decoration);
  }
}

resulting request refused to load.

After I added a

set { }

it worked.

Any piece of a clue?


Solution

  • Readonly properties are not serialized. Because when they will be deserialized they will not have setter. To avoid that issue, read-only properties are not serialized in the first place.

    Same case when set is private like

    public List<Foo> Bar {get; private set;}`.
    

    Read
    Why are properties without a setter not serialized
    Force XML serialization to serialize readonly property
    Why isn't my public property serialized by the XmlSerializer?

    Compiler is not concerned whether you have written a logic inside setter. All it cares is that your property should have a setter.