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 handled if I subclass the service method? Or will it reset all additional properties that are not in the base class?

ExtendedMessage msg = new ExtendedMessage();
...
ProcessMessage(msg);
+3
source share
2 answers

I think that if you did not specify ExtendedMessage through the KnownType attribute, you will get an error. After you tell WCF about ExtendedMessage through KnownType, it will work without data loss.

By the way, you do not need to know the set of possible types at compile time, because the KnownType attribute can refer to a method that will return a set of possible types at runtime.

+3
source

ServiceKnownType, KnownType. ServiceKnownType .

0

Source: https://habr.com/ru/post/1704752/


All Articles