0

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?

JNF
  • 3,696
  • 3
  • 31
  • 64
  • 2
    Readonly properties are not serialized. Because when they be deserialized back they will not have setter. Read this. http://stackoverflow.com/questions/13401192/why-are-properties-without-a-setter-not-serialized – Nikhil Agrawal May 19 '15 at 08:03
  • `set{}` does not make it unreadonly – JNF May 19 '15 at 08:03
  • Compiler is not concerned whether you have written a logic inside it. All it cares is that your property should have a setter. – Nikhil Agrawal May 19 '15 at 08:04

1 Answers1

3

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.

Community
  • 1
  • 1
Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208