c# - WCF:如何为类型中的类型构造 DataContract

标签 c# wcf datacontract

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

函数

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

这段代码是否足以确保调用客户端知道初始响应类型中包含的标准响应类型的结构?我的意思是说嵌套类型的数据契约是否真的会被遵守?

我应该补充一点,我对使用数据契约(Contract)还很陌生,因为我以前知道双方都有 .net。

最佳答案

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

关于c# - WCF:如何为类型中的类型构造 DataContract,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11990086/

相关文章:

c# - Silverlight 错误找不到类型或命名空间名称 'MatchTimeoutInMilliseconds'

WCF 客户端 - 为什么会出现此错误?

c# - 如何将 IOperationInvoker 添加到 wcf 客户端

asp.net - 从 wsdl 生成 Web 服务

Json 解析 F#

c# - 如何解决无法在 lambda 中使用引用的问题?

c# - 在 C# 中使用按钮事件后是否必须取消订阅?

C# 解决方案,除必须是 x86 的项目外,所有项目都可以针对 AnyCPU 吗?

regex - 具有 RegEx 列表的 WCF DataContract 无法正确序列化

c# - 设计 SOA WCF Web 服务时的最佳实践是什么?