Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF and subclasses

Tags:

c#

.net

wcf

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);
like image 935
Nick Avatar asked Feb 01 '26 08:02

Nick


1 Answers

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.

like image 91
Daniel Pratt Avatar answered Feb 02 '26 23:02

Daniel Pratt