GDS
GDS

Reputation: 1387

Wcf web service, reused type with private setter property and no DataContract/DataMember gets null values

I have created a WCF web service which is referenced in a Winforms UI and both projects reference a common class library with POCOs.

The service reference is configured to reuse types and everything works perfectly until I make a property's setter in one of the POCOs private. When passing that POCO as a parameter from the client to the server the property becomes null even though it was not null on the client.

As a workaround I had to explicitly declare [DataContract] and [DataMember] attributes on that particular POCO to get the property working again. (The other POCOs - all with public getters and setters - did not need any attributes).

Does anyone know:

1) why this happens (i.e. is this a bug or is it documented behaviour and if so what is the reasoning)?

and

2) whether there is a simpler work-around than decorating the class with attributes (e.g. some setting on the service etc.)?

Upvotes: 0

Views: 379

Answers (1)

LoekD
LoekD

Reputation: 11470

  1. If datamembers are not decorated and no datacontract attribute is present, all members that have public get and public set participate in serialization. When adding the attributes, anything can participate. (private fields too, for instance) more info
  2. In datacontracts, use only public get and public set. Apply business rules like access control on business objects.

Upvotes: 1

Related Questions