在.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方法,并没有编译时或运行时的有效验证。

相关文章:

  • 2022-12-23
  • 2021-10-13
  • 2022-12-23
  • 2021-07-13
  • 2021-09-10
  • 2021-11-25
猜你喜欢
  • 2021-08-03
  • 2021-12-16
  • 2021-06-29
  • 2021-11-05
  • 2021-12-24
  • 2022-01-03
  • 2021-06-01
相关资源
相似解决方案