我有一个 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。
最佳答案
是的,您可以让 DataContract
由 DataContract
s 组成。 WCF 将知道如何正确地序列化它们。
关于c# - WCF:如何为类型中的类型构造 DataContract,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11990086/