Angelo Lee's Blog
This is my kingdom .If i don't fight for it ,who will ?

本文的出发点:

通过阅读本文,能解决以下的问题

  1. 什么是契约?
  2. 契约有几种?,他们都有什么用途
  3. 如何定义契约?
  4. 契约是独立于平台的么?
  5. 契约和以往哪种技术比较相像,又有什么不同?

     

本文适合的读者

WCF的初学者,可以没有任何SOA或者 其他分布式技术经验

 

什么是契约?

    任何一个分布式应用程序,它之所以能够互相传递消息,都是事先制定好数据交换规则的 ,这个规则正是交换数据的双方(比如服务器端和客户端)能彼此理解对方的依据,WCF作为分布式开发技术的一种,同样具有这样一种特性。而在WCF中制定的的规则就被称之为契约(Contract) ,它是WCF的消息标准, 是任何一个wcf程序不可或缺的一部分。

 

契约有几种?,他们都有 什么用途

在WCF中,契约分为四种,它们分别为:

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

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

    ServiceContract用于类或者结构上,用于指示WCF此类或者结构能够被远程调用 ,而OperationContract用于类中的方法 (Method)上,用于指示WCF该方法可被远程调用。

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

    数据契约也分为两种:DataContract和DataMember
    DataContract用于类或者结构上,指示 WCF此类或者结构能够被序列化并传输 ,而DataMember只能用在类或者结构的属性(Property)或者字段(Field) 上,指示WCF该属性或者字段能够被序列化传输。

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

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

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

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

     

如何定义契约?

     事实上,契约的使用是很简单的,它无非就是在普通的程序结构上添加一些声明性的属性就可以了,比如我们可以直接在类上声明ServerContractAttribute,此时这个类就能被远程客户端访问到,而在类中的方法中 (Method)添加OperationContractAttribute就能将方法暴露给远程客户端,其他的契约也一样的用法,比较难的还是消息契约和错误契约,当也很简单。您仍不明白的话,可以参考下面几篇文章:

http://www.cnblogs.com/artech/archive/2007/02/28/659331.html

http://www.rainsts.net/article.asp?id=427

http://www.rainsts.net/article.asp?id=429

http://www.rainsts.net/article.asp?id=430

他们的blog中都有较详细的阐述和使用 方法说明

 

契约是独立于平台的么?

    WCF作为一种能够跨平台的体系框架,其应用肯定会有异构,异网的情况发生,那么作为通讯依据的契约能否自动适用于上述情况呢?答案是肯定的,契约是独立于平台之外的, 它只约束通讯的双方应该遵守什么样的规则,而丝毫不管双方各自采用的是什么样的技术和什么样的操作系统,也只有这样,WCF才能有真正的生命力。

 

契约和以往哪种技术比较相像,又有什么不同?

    如果非要拿契约和以往的技术相比较的话,契约和asp.net xml web service的声明性编程模型甚是相似 ,比如在web service中在类上标记WebServiceAttribute便可以将此类用于远程调用,而将方法添加WebMethondAttribute也可 以将其暴露给远程客户端,这和WCF中的ServiceContract和OperationContract简直如出一辙,但不同的是,WCF中的契约 要比Xml Web Service中的要详尽的多,比如ServiceContract和OperationContract可以直接使用在接口上面,而实现该接口的类就继 承了这种契约声明,自动拥有契约所规范的动作和行为,这就使得程序员更方便的使用面向接口的编程方式,可以使同一服务拥有不同的实现,在新旧版本升级的同时,能够使新老版本共同运行。



本文的参考资料

http://www.cnblogs.com/artech/archive/2007/02/28/659331.html

http://www.rainsts.net/article.asp?id=427

http://www.rainsts.net/article.asp?id=429

http://www.rainsts.net/article.asp?id=430

http://www.cnblogs.com/wayfarer/archive/2006/04/10/370957.html

 

What are contracts in WCF?

Answer:
In WCF, all services expose contracts. The contract is a platform-neutral and standard way of describing what the service does.

WCF defines four types of contracts.


Service contracts

Describe which operations the client can perform on the service.
There are two types of Service Contracts.
ServiceContract - This attribute is used to define the Interface.
OperationContract - This attribute is used to define the method inside Interface.

 

Data contracts

Define which data types are passed to and from the service. WCF defines implicit contracts for built-in types such as int and string, but we can easily define explicit opt-in data contracts for custom types.

There are two types of Data Contracts.
DataContract - attribute used to define the class
DataMember - attribute used to define the properties.

 


If DataMember attributes are not specified for a properties in the class, that property can't be passed to-from web service.

Fault contracts

Define which errors are raised by the service, and how the service handles and propagates errors to its clients.


Message contracts

Allow the service to interact directly with messages. Message contracts can be typed or untyped, and are useful in interoperability cases and when there is an existing message format we have to comply with.

posted on 2010-11-30 16:37  Angelo Lee  阅读(748)  评论(0编辑  收藏  举报