tags:

views:

209

answers:

1
+2  Q: 

WCF and subclasses

I have this ServiceContract

[OperationContract(IsOneWay=true)]
void ProcessMessage(Message message);

and these objects

[DataContract]
public class Message
{
    [DataMember]
    public long Id { get; set; }

    [DataMember]
    public string Body { get; set; }
}

[DataContract]
public class ExtendedMessage : Message
{       
    [DataMember]
    public NameValueCollection AdditionalData { get; set; }
}

Will WCF be able to handle if I pass in the subclass to the service method? Or will it drop all extra properties that aren't on the base class?

ExtendedMessage msg = new ExtendedMessage();
...
ProcessMessage(msg);
+2  A: 

I think if you didn't specify ExtendedMessage via the KnownType attribute, you would get an error. Once you've told WCF about ExtendedMessage via KnownType, it will work without losing data.

By the way, you don't need to know the set of possible types at compile time because the KnownType attribute can reference a method that will return the set of possible types at runtime.

Daniel Pratt