ROBERT RICHARDSON ROBERT RICHARDSON - 1 year ago 52
C# Question

How to include a class instance in a WCF data contract?

I am trying to set up a WCF service to pass information about a complicated object, SimCalibrationData, to a client. SimCalibratonData contains a list of objects of type SimCalibrationBaseData. SimCalibrationBaseData has objects of type SimPlateData. All classes are decorated with [DataContract], and public properties are decorated with [DataMember]. If I wrap the SimPlateData objects into a List<>, I can send them to my client and they are successfully deserialized. If I don't wrap them, I get an error saying "The server did not provide a meaningful reply" and a suggestion that there might be a contract mismatch.

I've read that lists and other collections can always be serialized and deserialized, but I haven't found anything telling me what to do if I don't want to go to the trouble wrapping all my data in lists.

Here is my server-side code:

[DataContract]
public class SimCalibrationData
{
private List<SimBaseCalibrationData> m_baseCalibrations = new List<SimBaseCalibrationData>();
[DataMember]
public List<SimBaseCalibrationData> BaseCalibrations
{
get { return m_baseCalibrations; }
}

public SimCalibrationData(CSimThermalCalibrationList calibrationList)
{
foreach (CSimThermalCalibration calibration in calibrationList.Listing)
{
m_baseCalibrations.Add(new SimBaseCalibrationData(calibration));
}
}
}

[DataContract]
[KnownType(typeof(SimPlateData))]
public class SimBaseCalibrationData
{
// Plate Parameters
SimPlateData m_convectorPlate;
[DataMember]
public SimPlateData ConvectorPlate
{
get { return m_convectorPlate; }
}
SimPlateData m_loadPlate;
[DataMember]
public SimPlateData LoadPlate
{
get { return m_loadPlate; }
}

public SimBaseCalibrationData(CSimThermalCalibration calibration)
{
m_convectorPlate = new SimPlateData(calibration.ConvectorPlate);
m_loadPlate = new SimPlateData(calibration.LoadPlate);
}
}

[DataContract]
public class SimPlateData
{
public SimPlateData(CSimPlate plate)
{
}
}


Thank you for your help. In the meantime, I'll be wrapping everything in lists.

Answer Source

Tim's first comment, pointing out that serialized properties must have setters, was the answer.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download