views:

40

answers:

1

Is the following concept possible, or will I have trouble serializing this to the Client. Assuming that all comms are only dealing with BaseContractClasses but the Server uses the special sub-class to collate extra server-only data.

[DataContract]
public class BaseContractClass
{
  [DataMember]
  public int valueToTransmit;
}

public class ServiceOnlyContractClass : BaseContractClass
{
  public int valueNotToTransmit;
}
A: 

This new class ServiceOnlyContractClass will inherit all the fields and everything from its base class, but since it does not have any [DataContract] or [DataMember] attributes, it won't show up in your service contract (the WSDL/XSD).

If that's what you want, yes, that's the way to do it. The DataContract and DataMember attributes are specific, e.g. you have to explicitly set them and they will not be inherited to derived classes.

UPDATE: .NET 3.5 Service Pack 1 introduced a new set of rules for the WCF DataContractSerializer, and it looks as if any public class and any of its public properties will be serialized by the DCS as of .NET 3.5 SP1. This was introduced to simplify life and allow serialization of POCO (Plain Old CLR Object) classes without decorating them with attributes - but at the same time, there doesn't seem to be a way to turn off that feature if you don't want it.....

For more details see DataContracts without attributes

marc_s
But can the client handle not knowing about ServiceOnlyContractClass? Can it be reassembled into a BaseContractClass even though a SeriveOnlyContractClass was sent? I thought it might try to serialize as:<ServiceOnlyContractClass> <valueToTransmit>..</valueToTransmit></ServiceOnlyContractClass>
Nicholas
If the type isn't serialized into your DataContract, the client will **not** know about it. Period. If you don't serialize that type into your contract, you cannot use it in any method call as a parameter. If you want to use that class, but prevent a single element from being used (the "valueNotToTransmit"), just don't make that value public and don't put a [DataMember] onto it.
marc_s