Click here to Skip to main content
11,715,160 members (65,501 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WCF NET
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 28-Sep-12 14:05pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
v2
Comments
Sergey Alexandrovich Kryukov at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
Comments
Sandeep Mewara at 1-Oct-12 14:47pm
   
comment from Op:
You are right Sergey Alexandrovich Kryukov, thank you.
Sergey Alexandrovich Kryukov at 1-Oct-12 14:51pm
   
@OP:
You are very welcome.
Good luck, call again.
--SA

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 495
1 Richard MacCutchan 275
2 Maciej Los 259
3 ManasKumarM 155
4 Frankie-C 125
0 Sergey Alexandrovich Kryukov 775
1 CHill60 415
2 Richard MacCutchan 405
3 Maciej Los 389
4 OriginalGriff 320


Advertise | Privacy | Mobile
Web01 | 2.8.150901.1 | Last Updated 1 Oct 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100