WCF:如何为类型中的类型构造DataContracts

时间:2012-08-16 15:01:20

标签: c# wcf datacontract

我有一个wcf服务,它公开一个返回复杂类型作为返回类型的函数。此响应类型又包含由我定义的另一个复杂对象。我正在考虑为我的WCF创建数据合同,我想知道如何做到这一点。我目前有这个(为了便于阅读,删除了一些属性):

功能

///<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);

响应类型

/// <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;
    }
}

嵌套类型

/// <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;
    }
}

此代码是否足以确保调用客户端知道初始响应类型中保存的标准响应类型的结构?我的意思是实际上是否会遵守嵌套类型的数据合同?

我应该补充一点,我使用数据合同是相当新的,因为我之前知道我有.net双方。

1 个答案:

答案 0 :(得分:4)

是的,您可以DataContracts组成DataContract。 WCF将知道如何正确序列化它们。