zoukankan      html  css  js  c++  java
  • WCF学习

    一、契约

          任何一个分布式应用程序,都会事先制定好数据交换规则,这个规则正是交换数据的双方(比如服务器端和客户端)能彼此理解对方的依据。在WCF中称之为契约(Contract)。

    1.契约的分类

        1)用于定义服务操作的服务契约:Service Contract

             这种级别的契约又包括两种:ServiceContract和OperationContract。

             ServiceContract用于类或结构上,用于指示WCF此类或者结构能够被远程调用。

             OperationContract用于类中的方法(Method)上,用于指示WCF该方法可被远程调用。

    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        string GetData(int value);

        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);
    }

        2)用于自定义数据结构的数据契约:Data Contract

             数据契约也分为两种:DataContract和DataMember。

             DataContract用于类或者结构上,指示WCF此类或者结构能够被序列化并传输。

             DataMember只能用在类或者结构的属性(Property)或者字段(Field)上,指示WCF该属性或字段能够被序列化并传输。

    [DataContract]
    public class CompositeType
    {
        bool boolValue = true;
        string stringValue = "Hello ";

        [DataMember]
        public bool BoolValue
        {
            get { return boolValue; }
            set { boolValue = value; }
        } 

        [DataMember]
        public string StringValue
        {
            get { return stringValue; }
            set { stringValue = value; }
        }
    }

        3)用于自定错误异常的异常契约:Fault Contract

             默认情况下,当服务器端抛出异常的时候,客户端能接收到异常信息的描述,但这些描述往往格式统一,有时比较难以从中获取有用的信息。此时,我们可以自定义异常信息的格式,将我们关心的消息放到错误消息中传递给客户端,此时需要在方法上添加自定义一个错误消息的类,然后在要处理异常的函数上加上FaultContract,并将异常信息指示返回为自定义格式。

    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        [FaultContract(typeof(OverflowException))]
        string GetData(int value);

        [OperationContract]
        [FaultContract(typeof(OverflowException))]
        CompositeType GetDataUsingDataContract(CompositeType composite);
    }

        4)用于控制消息格式的消息契约:Message Contract

             能自定义消息格式,包括消息头,消息体,还能指示是否对消息内容进行加密和签名。

    [MessageContract(IsWrapped = true, ProtectionLevel = ProtectionLevel.EncryptAndSign)]
    public class MyMessage
    {
        string key;
        CompositeType composite;

        [MessageHeader]
        public string key
        {
            get { return key; }
            set { key = value; }
        }

        [MessageBodyMember]
        public CompositeType Composite
        {
            get { return composite; }
            set { composite = value; }
        }
    }

    2.契约独立于平台

        它只约束通讯的双方应该遵守什么样的规则,而丝毫不管双方各自采用的是什么样的技术和什么样的操作系统。 

    二、 Address

          Address通过一个Uri来唯一标示一个WCF的终节点(EndPoint)的,它标示了消息发送的目的地。

    1.Address的组成

          Address在WCF中用System.ServiceModel.EndpoingAddress对象来表示的,它的结构如下:

         

    2.在配置文件中指定Address

          在配置文件中,有两种方式可以指定Address,一种是绝对地址方式,另一种是相对地址方式。

    3.通过编程方式设置Address

    三、Binding

          Endpoing包括三个组成部分:1.Address 2.Binding 3.Contract

          Address解决了Endpoint在哪的问题。

          Binding解决的是如何与Endpoint通讯的问题。

          Contract解决了Endpoint提供什么功能的问题。

          Binding实现的是客户端和服务端进行通讯的底层细节,包括数据如何传输(比如采用TCP,HTTP等),如何解决安全问题(SSL、Message Level Security等),传输的消息的格式(如text/xml、MTOM、binary等)。

    1.Binding的组成

          Binding包括三个组成部分:NameSpace、Name和BindingElement。

          Name和NameSpace是服务元数据(meta data)的唯一标志,二者就像Binding的姓名一样,而BindingElement则描述Binding的特征,我们说Binding能解决通讯方式的问题,关键是靠BindingElement来进行描述。

    2.BindingElement的分类

          BindingElement分为三种类型:

          1)EncodingBindingElement:它用于描述传输数据的编码方式,比如用text/xml进行编码,用binary编码,用MTOM进行编码都是在这个上面定义,每个BindingElement必须包含一个EncodingBindingElement。

          2)TransportBindingElement:它用于描述数据的传输方式,例如使用tcp进行传输,还是用http进行传输,还是用msmq,这些都是由TransportBindingElement来定义,每个BindingElement必须包含一个TransportBindingElement。

          3)ProtocolBindingElement:指定诸如安全性、可靠性、上下文流设置(context flow settings)。

    3.Binding描述哪些层面的信息

         

    4.选择正确的Binding 

           

           

           

  • 相关阅读:
    [LeetCode] 304. Range Sum Query 2D
    [LeetCode] 303. Range Sum Query
    [Google] Help employee find the nearest gbike
    Difference between Process and thread?
    Given a family tree, find out if two people are blood related
    [LeetCode] 676. Implement Magic Dictionary 实现神奇字典
    [LeetCode] 659. Split Array into Consecutive Subsequences 将数组分割成连续子序列
    [LeetCode] 815. Bus Routes 公交路线
    [LeetCode] 129. Sum Root to Leaf Numbers 求根到叶节点数字之和
    mybatis example 使用AND 和OR 联合查询
  • 原文地址:https://www.cnblogs.com/pennant/p/2749071.html
Copyright © 2011-2022 走看看