Click here to Skip to main content
15,894,405 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
I have derived class "bar" that has additional fields to the base class. The WCF service must accept the base class type (because there are several derived classes that have to use the same service, each derived class will have additional fields). I need to transfer the derived class's fields over WCF, below is an example.

[KnownType(typeof(bar))]
[DataContract]
public class fooBase
{
[DataMember]
    public string a;
}

[DataContract]
public class bar : fooBase
{
    [DataMember]
    public string b;
}


My WCF service accepts fooBase. When the client passes an instance of bar to the service, a is populated with the correct data, but b is always null.

Is it possible for WCF to transfer the fields of derived classes? If so, how? If not, is this possible with Remoting?

Thanks!
Posted

Everything is correct. I think, you just never assigned a value to this field, that's it. Manually write the value in XML file and read it with the data contract serializer to see that it appears.

One little recommendation: always use the namespace on the root object of the data contract: it will make your data model meta-data (and data format) world-unique, if the namespace is based on world-unique URL of course.

—SA
 
Share this answer
 
Comments
Sandeep Mewara 1-Oct-12 14:47pm    
comment from Op:
You are right Sergey Alexandrovich Kryukov, thank you.
Sergey Alexandrovich Kryukov 1-Oct-12 14:51pm    
@OP:
You are very welcome.
Good luck, call again.
--SA
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 28-Sep-12 23:58pm    
This is good interesting reading, something good to know, thank you for sharing, my 5.
At the same time, there is nothing wrong with OP's example -- inherited member is already included, so I think the problem is simple -- please see my answer.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900