【问题标题】:WCF: How to construct DataContracts for a type within a typeWCF:如何为类型中的类型构造 DataContract
【发布时间】:2012-08-16 15:01:20
【问题描述】:

我有一个 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 双方。

【问题讨论】:

  • 你为什么不通过创建一个测试客户端来试试呢?

标签: c# wcf datacontract


【解决方案1】:

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

【讨论】:

  • 所以理论上你可以在类型中拥有任意数量的嵌套类型?如果您为各个类提供数据协定,直到您定义了所有可能的类型并且 wcf 将知道如何序列化/反序列化它们?
  • 只是一个xml,如果xml有任何限制,我不知道,我认为它应该可以工作。
  • 是的,尽管您显然想对合同的复杂程度有所了解。 DataContracts 实际上是用于与服务器交换消息的简单数据结构。在相关的说明中,您还应该避免将数据合同上的任何属性定义为接口。仅对您的 DataContracts 使用具体类型。
  • 对于上面的示例,您应该没问题,但取决于您的对象嵌套的深度以及它们的错误程度(当呈现为 XML 时),您可能需要调整 WCF 绑定。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多