Search code examples
c#wcfdatacontract

WCF: How to construct DataContracts for a type within a type


I have a wcf service which exposes a function that returns a complex type as its return type. This response type in turn contains another complex object defined by me. I am looking at creating data contracts for my WCF and am wondering how this is supposed to be done. I currently have this (some properties removed for ease of reading):

The Function

///<summary>
/// Interface to describe a cluster query WCF service.
///</summary>
[ServiceContract]
public interface IClusterQueryWcfService
{
    /// <summary>
    /// A method to retrieve the name of the necessary cluster table for a given zoom level, feature type and user type. 
    /// </summary>
    /// <param name="zoom">Integer value representing zoom level</param>
    /// <param name="featureType">The feature type string</param>
    /// <param name="user">User name</param>
    /// <returns>RwolTableType made up of table name and table type.(See documentation)</returns>
    [OperationContract]
    TableTypeResponse GetClusterTableForZoom(int zoom, string featureType, string user);

Response Type

/// <summary>
/// The data contract for a TableTypeResponse object
/// </summary>
[DataContract]
public class TableTypeResponse
{
    /// <summary>
    /// Property to manipulate the date and time of the method call.
    /// </summary>
    [DataMember]
    public string DateTimeOfCall
    {
        get;
        set;
    }

    /// <summary>
    /// Property to get/set the StandardResponse type object included with a TableTypeResponse instance.
    /// </summary>
    [DataMember]
    public StandardResponseType StandardResponse
    {
        get;
        set;
    }
}

Nested Type

/// <summary>
/// Data contract for a StandardResponseType object
/// </summary>
[DataContract]
public class StandardResponseType
{
    /// <summary>
    /// Property to manipulate the date and time of the method call.
    /// </summary>
    [DataMember]
    public string DateTimeOfCall
    {
        get;
        set;
    }

    /// <summary>
    /// Property to allow get and set of a message to provide more information to the user.
    /// </summary>
    [DataMember]
    public string Message
    {
        get;
        set;
    }
}

Is this code enough to make sure that the calling client knows the structure of the Standard response type held within the initial Response type? By which I mean will the data contract for the nested type actually be observed?

I should add I am fairly new to using data contracts as previously I knew I had .net both sides.


Solution

  • Yes, you can have DataContracts made up of DataContracts. WCF will know how to serialize them properly.