zoukankan      html  css  js  c++  java
  • WCF学习笔记(六)WCF基础

    集合(Collections)

    在.Net中,各种类型的集合均实现了IEnumerable或IEnumerable<T>接口。.Net集合是.Net特有的,WCF中不能在服务元数据中公开他们。

    定义服务操作时,不管使用哪种结合接口,他们的传输表现形式都使用了数组。

    [ServiceContract]
        interface IContractManager
        {
            //不能接受Customer对象
            [OperationContract]
            void AddContract(Contract contract);
            
            //不能返回Customer对象
            [OperationContract]
            IEnumerable<Contract> GetContracts();
        }

    导出的结果为

    [ServiceContract]
        interface IContractManager
        {
            //不能接受Customer对象
            [OperationContract]
            void AddContract(Contract contract);
            
            //不能返回Customer对象
            [OperationContract]
            Contract[] GetContracts();
        }

    如果契约中的集合不是接口,而是直接的集合类型,而且属于可序列化集合,只要提供的结合包含了Add()方法,并且Add方法符合下面一种签名,WCF就能够自动地将集合规范为数组类型。 

    public void Add(object obj);

    public void Add(T item);

    集合只需要包含Add方法,但不需要实现Add方法。

    正如前面所惯有的风格,WCF为了能够最大程度解耦,为了不与.Net有太多瓜葛,WCF总是为自己出一套理想的特性。

    CollectionDataContract特性

     前面所示的编组为具体集合类型的机制并不理想。第一,它要求集合必须可序列化。第二,服务的一方处理集合,一方却在处理数组。第三,对于集合是否包含Add方法,并没有编译时或运行时的有效验证。

  • 相关阅读:
    js中==与===区别
    Initialization failure 0x0000000c
    Spring通过@Value注解注入属性的几种方式
    java中读取配置文件中数据的具体方法
    spring整合hibernate
    url上参数解析笔记
    编号的生成(日期+序列号)
    application.xml & -servlet.xml
    webApplicationContext 与servletContext
    Http协议整理
  • 原文地址:https://www.cnblogs.com/HelloMyWorld/p/3064653.html
Copyright © 2011-2022 走看看