谈谈WCF中的Data Contract(3):WCF Data Contract对Collection & Dictionary的支持

在本篇文章上一部分Order Processing的例子中,我们看到原本已Collection形式定义的DetailList属性(public IList<TDetail> DetailList),在Data Contract中却以Array的方式体现(public OrderDetail[] DetailList)。我们现在就来详细地讨论一下基于Collection & Dictionary 的Data Contract。

Data Contract for Collection

我们照例用例子来说明问题,在这里我们创建一个批量处理Order的Service,于是我们创建了一个OrderCollection Type:

namespace Artech.SpecialDataContract.Contract
{
    [DataContract]
    public class Order
    {
        [DataMember]
        public Guid OrderID
        { get; set; }

        [DataMember]
        public DateTime OrderDate
        { get; set; }
    }

    public class OrderCollection : List<Order>
    {

    }
}

下面是Service Contract的定义:

namespace Artech.SpecialDataContract.Contract
{
    [ServiceContract]
    public interface IOrderManager
    {
        [OperationContract(Name = "ProcessWithCollection")]
        void Process(OrderCollection orders);
}

面是OrderCollection 在XSD中的呈现:

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema elementFormDefault="qualified" targetNamespace="http://schemas.datacontract.org/2004/07/Artech.SpecialDataContract.Contract"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://schemas.datacontract.org/2004/07/Artech.SpecialDataContract.Contract"
    xmlns:ser="http://schemas.microsoft.com/2003/10/Serialization/">
  <xs:import schemaLocation="http://artech/Artech.SpecialDataContract/OrderManagerService.svc?xsd=xsd1"
          namespace="http://schemas.microsoft.com/2003/10/Serialization/" />
  <xs:complexType name="ArrayOfOrder">
    <xs:sequence>
      <xs:element minOccurs="0" maxOccurs="unbounded" name="Order" nillable="true" type="tns:Order" />
    </xs:sequence>
  </xs:complexType>
  <xs:element name="ArrayOfOrder" nillable="true" type="tns:ArrayOfOrder"/>
  <xs:complexType
name="Order">
    <xs:sequence>
      <xs:element minOccurs="0" name="OrderDate" type="xs:dateTime"/>
      <xs:element
minOccurs="0" name="OrderID" type="ser:guid"/>
    </xs:sequence>
  </xs:complexType>
  <xs:element
name="Order" nillable="true" type="tns:Order"/>
</xs:schema>

加上通过Add Service Reference默认生成的Class,我们可以很清楚地看出Collection是以Array的形式呈现的(Artech.SpecialDataContract.Client.OrderManagerService.Order[] orders):

    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
    [System.ServiceModel.ServiceContractAttribute(ConfigurationName="OrderManagerService.IOrderManager")]
    public interface IOrderManager { 
        [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IOrderManager/ProcessWithCollection", ReplyAction="http://tempuri.org/IOrderManager/ProcessWithCollectionResponse")]
        void ProcessWithCollection(Artech.SpecialDataContract.Client.OrderManagerService.Order[] orders);
}

因为Array相对很Common的数据类型,基本上所有的厂商均提供了对Array的支持,这也是WCF在通过Add Service Reference生成Client端代码的时候,会生成Array的原因。不过并不是我们只有唯一的选择,事实上VS为此提供了扩展,允许我们对于基于Collection 的Data Contract生成我们需要的各种类型,我们只需要在Add Service Reference的时候选择“Configure Service Reference”进行相应的配置:

通过上面的截图,我们发现在Collection Type一项我们有若干选项,我们可以选择我们希望生成的数据类型:Array,ArrayList,LinkedList,Generic List,Collection和BindingList。 

Data Contract for Dictionary

前面的内容,我们分别讨论了基于Generic和Collection的Data Contract,接下来,我们来讨论最后一个特殊的数据类型的Data Contract:Dictionary。

延续上面的Order Batch Processing的例子,不过我们现在处理的不是一个OrderCollection对象,而是一个Dictionary对象,线面是Service Contract和Order的定义:

namespace Artech.SpecialDataContract.Contract
{
    [ServiceContract]
    public interface IOrderManager
    {
        [OperationContract(Name = "ProcessWithCollection")]
        void Process(OrderCollection orders);

        [OperationContract(Name = "ProcessWithDictionary")]
        void Process(IDictionary<Guid, Order> orders);
    }
}
    [DataContract]
    public class Order
    {
        [DataMember]
        public Guid OrderID
        { get; set; }

        [DataMember]
        public DateTime OrderDate
        { get; set; }
    }

闲话少说,我们来看XSD:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified" targetNamespace="http://schemas.microsoft.com/2003/10/Serialization/Arrays" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://schemas.microsoft.com/2003/10/Serialization/Arrays" xmlns:ser="http://schemas.microsoft.com/2003/10/Serialization/">
  <xs:import schemaLocation="http://artech/Artech.SpecialDataContract/OrderManagerService.svc?xsd=xsd1" namespace="http://schemas.microsoft.com/2003/10/Serialization/"/>
  <xs:import schemaLocation="http://artech/Artech.SpecialDataContract/OrderManagerService.svc?xsd=xsd2" namespace="http://schemas.datacontract.org/2004/07/Artech.SpecialDataContract.Contract"/>
  <xs:complexType name="ArrayOfKeyValueOfguidOrder_SkVQi6O3">
    <xs:annotation>
      <xs:appinfo>
        <IsDictionary xmlns="http://schemas.microsoft.com/2003/10/Serialization/">true</IsDictionary>
      </xs:appinfo>
    </xs:annotation>
    <xs:sequence>
      <xs:element minOccurs="0" maxOccurs="unbounded" name="KeyValueOfguidOrder_SkVQi6O3">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="Key" type="ser:guid"/>
            <xs:element name="Value" nillable="true" type="q1:Order" xmlns:q1="http://schemas.datacontract.org/2004/07/Artech.SpecialDataContract.Contract"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>
  <xs:element name="ArrayOfKeyValueOfguidOrder_SkVQi6O3" nillable="true" type="tns:ArrayOfKeyValueOfguidOrder_SkVQi6O3"/>
</xs:schema>

Data Contract的名称为ArrayOfKeyValueOfguidOrder_SkVQi6O3=ArrayOfKeyValueOf+guid(Key的类型)+Order(Value)+_SkVQi6O3(Hash Value)。从该XSD的结构我们不难看出,只是一个数组,每个元素为Key-Value pair。

我们照例看看通过Add Service Reference方式生成的Client端code中的对应的定义:

    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
    [System.ServiceModel.ServiceContractAttribute(ConfigurationName="OrderManagerService.IOrderManager")]
    public interface IOrderManager {               
        [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IOrderManager/ProcessWithDictionary", ReplyAction="http://tempuri.org/IOrderManager/ProcessWithDictionaryResponse")]
        void ProcessWithDictionary(System.Collections.Generic.Dictionary<System.Guid, Artech.SpecialDataContract.Client.OrderManagerService.Order> orders);
    }

生成的是一个System.Collections.Generic.Dictionary类型。同Collection一样,也依然可以有多种选择:

谈谈WCF中的Data Contract(3):WCF Data Contract对Collection & Dictionary的支持的更多相关文章

  1. WCF初探-26:WCF中的会话

    理解WCF中的会话机制 在WCF应用程序中,会话将一组消息相互关联,从而形成对话.会话”是在两个终结点之间发送的所有消息的一种相互关系.当某个服务协定指定它需要会话时,该协定会指定所有调用(即,支持调 ...

  2. WCF中配置文件解析

    WCF中配置文件解析[1] 2014-06-14 WCF中配置文件解析 参考 WCF中配置文件解析 返回 在WCF Service Configuration Editor的使用中,我们通过配置工具自 ...

  3. wcf中的使用全双工通信(转)

    wcf中的使用全双工通信   wcf中的契约通信默认是请求恢复的方式,当客户端发出请求后,一直到服务端回复时,才可以继续执行下面的代码. 除了使用请求应答方式的通信外,还可以使用全双工.下面给出例子: ...

  4. WCF学习之旅—WCF中传统的异常处理(十六)

    WCF中的异常处理 在软件开发过程中,不可能没有异常的出现,所以在开发过程中,对不可预知的异常进行解决时,异常处理显得尤为重要.对于一般的.NET系统来说,我们简单地借助try/catch可以很容易地 ...

  5. wcf中的使用全双工通信

    wcf中的契约通信默认是请求恢复的方式,当客户端发出请求后,一直到服务端回复时,才可以继续执行下面的代码. 除了使用请求应答方式的通信外,还可以使用全双工.下面给出例子: 1.添加一个wcf类库 2. ...

  6. [No0000126]SSL/TLS原理详解与WCF中的WS-Security

    SSL/TLS作为一种互联网安全加密技术 1. SSL/TLS概览 1.1 整体结构 SSL是一个介于HTTP协议与TCP之间的一个可选层,其位置大致如下: SSL:(Secure Socket La ...

  7. 理解WCF中的Contracts

    WCF中的Contracts WCF通过Contract来说明服务和操作,一般包含五种类型的Contract:ServiceContract,OperationContract,FaultContra ...

  8. 浅议Grpc传输机制和WCF中的回调机制的代码迁移

    浅议Grpc传输机制和WCF中的回调机制的代码迁移 一.引子 如您所知,gRPC是目前比较常见的rpc框架,可以方便的作为服务与服务之间的通信基础设施,为构建微服务体系提供非常强有力的支持. 而基于. ...

  9. WCF中,通过C#代码或App.config配置文件创建ServiceHost类

    C# static void Main(string[] args) { //创建宿主的基地址 Uri baseAddress = new Uri("http://localhost:808 ...

随机推荐

  1. JavaScript之字符串

    一.声明方式 1. 直接赋值 var str = 'hello javascript'; 2. 构造函数 var str2 = new String('hello world'); 这两种有什么区别呢 ...

  2. Aborting a running program

    In the event that a calculation appears to be running excessively long, one can abort thecalculation ...

  3. linux IO子系统和文件系统读写流程

        本文转载自:http://blog.csdn.net/kidd_3/article/details/6909097 Technorati 标签: I/O 子系统 --------------- ...

  4. VKP5 Price Calculation – List Variant & KZPBL (Delete site level)

    List Variant: Configuration in Logistic General –> Retail Pricing –> Sales Price Calculation – ...

  5. Firefox 与 IE 对Javascript和CSS的区别

    1. document.formName.item("itemName") 问题 说明:IE下,可以使用document.formName.item("itemName& ...

  6. MySQL 性能优化

    内容简介:这是一篇关于mysql 性能,mysql性能优化,mysql 性能优化的文章.网上有不少mysql 性能优化方案,不过,mysql的优化同sql server相比,更为麻烦与负责,同样的设置 ...

  7. 将word中的“空格” 转换为换行符

  8. VS2012中进行Web性能和负载测试

    问题1:无法使用ie进行录制 解决方法: 工具 >> 管理加载项 >> 在工具栏和扩展中找到发布者为Microsoft Corporation的Microsoft Web Te ...

  9. PHP操作MongoDB简明教程(转)

    转自:http://blog.sina.com.cn/s/blog_6324c2380100ux2m.html MongoDB是最近比较流行的NoSQL数据库,网络上关于PHP操作MongoDB的资料 ...

  10. linux下配置Apache基于加密的认证访问

    1.首先要确认安装了 mod_ssl模块 我的机器是centos是系统,执行下面命令 yum install -y mod_ssl 2.用openssl工具生成密钥,证书请求文件,证书 在/usr/l ...