自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

揭秘四種行為WCF接口使用

開發(fā) 后端
文章主要介紹了WCF的四種行為:服務(wù)行為、終結(jié)點(diǎn)行為、契約行為和操作行為,還介紹了這四種行為下的四個(gè)WCF接口。

接口是實(shí)現(xiàn)項(xiàng)目的若耦合的,是程序員***用的,WCF有四個(gè)常見的接口,下面我們就來(lái)詳細(xì)的看看。WCF提供了四種類型的行為:服務(wù)行為、終結(jié)點(diǎn)行為、契約行為和操作行為。這四種行為分別定義了四個(gè)WCF接口:IServiceBehavior,IEndpointBehavior,IContractBehavior以及IOperationBehavior。

#T#是四個(gè)不同的WCF接口,但它們的接口方法卻基本相同,分別為AddBindingParameters(),ApplyClientBehavior()以及ApplyDispatchBehavior()。注意,IServiceBehavior由于只能作用在服務(wù)端,因此并不包含ApplyClientBehavior()方法。我們可以定義自己的類實(shí)現(xiàn)這些WCF接口,但需要注意幾點(diǎn):
1、行為的作用范圍,可以用如下表格表示:
2、可以利用自定義特性的方式添加擴(kuò)展的服務(wù)行為、契約行為和操作行為,但不能添加終結(jié)點(diǎn)行為;可以利用配置文件添加擴(kuò)展服務(wù)行為和終結(jié)點(diǎn)行為,但不能添加契約行為和操作行為。但這些擴(kuò)展的行為都可以通過(guò)ServiceDescription添加。

利用特性添加行為,意味著我們?cè)诙x自己的擴(kuò)展行為時(shí),可以將其派生自Attribute類,然后以特性方式添加。例如:

  1. [AttributeUsage(AttributeTargets.Class|AttributeTargets.Interface)]  
  2. publicclassMyServiceBehavior:Attribute,IServiceBehavior...  
  3. [MyServiceBehavior]  
  4. publicinterfaceIService... 

如果以配置文件的方式添加行為,則必須定義一個(gè)類繼承自BehaviorExtensionElement(屬于命名空間System.ServiceModel.Configuration),然后重寫屬性BehaviorType以及CreateBehavior()方法。BehaviorType屬性返回的是擴(kuò)展行為的類型,而CreateBehavior()方法則負(fù)責(zé)創(chuàng)建該擴(kuò)展行為的對(duì)象實(shí)例:

  1. publicclassMyBehaviorExtensionElement:BehaviorExtensionElement  
  2. {  
  3. publicMyBehaviorExtensionElement(){}  
  4. publicoverrideTypeBehaviorType  
  5. {  
  6. get{returntypeof(MyServiceBehavior);}  
  7. }  
  8.  
  9. protectedoverrideobjectCreateBehavior()  
  10. {  
  11. returnnewMyServiceBehavior();  
  12. }  
  13. }  

如果配置的Element添加了新的屬性,則需要為新增的屬性應(yīng)用ConfigurationPropertyAttribute,例如:

  1. [ConfigurationProperty("providerName",IsRequired=true)]  
  2. publicvirtualstringProviderName  
  3. {  
  4. get  
  5. {  
  6. returnthis["ProviderName"]asstring;  
  7. }  
  8. set  
  9. {  
  10. this["ProviderName"]=value;  
  11. }  

配置文件中的配置方法如下所示:

  1. <configuration> 
  2. <system.serviceModel> 
  3. <services> 
  4. <servicenameservicename="MessageInspectorDemo.Calculator"> 
  5. <endpointbehaviorConfigurationendpointbehaviorConfiguration="messageInspectorBehavior" 
  6. address="http://localhost:801/Calculator" 
  7. binding="basicHttpBinding" 
  8. contract="MessageInspectorDemo.ICalculator"/> 
  9. </service> 
  10. </services> 
  11. <behaviors> 
  12. <serviceBehaviors> 
  13. <behaviornamebehaviorname="messageInspectorBehavior"> 
  14. <myBehaviorExtensionElementproviderNamemyBehaviorExtensionElementproviderName="Test"/> 
  15. </behavior> 
  16. </serviceBehaviors> 
  17. </behaviors> 
  18. <extensions> 
  19. <behaviorExtensions> 
  20. <addnameaddname="myBehaviorExtensionElement" 
  21. type="MessageInspectorDemo.MyBehaviorExtensionElement,MessageInspectorDemo,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null"/> 
  22. </behaviorExtensions> 
  23. </extensions> 
  24. </system.serviceModel> 
  25. </configuration> 

注意,在<serviceBehaviors>一節(jié)中,<behavior>下的<myBehaviorExtensionElement>就是我們擴(kuò)展的行為,providerName則是MyBehaviorExtensionElement增加的屬性。如果擴(kuò)展了IEndpointBehavior,則配置節(jié)的名稱為<endpointBehaviors>。<extensions>節(jié)負(fù)責(zé)添加自定義行為的擴(kuò)展。其中,<add>中的name值與<behavior>下的<myBehaviorExtensionElement>對(duì)應(yīng)。

責(zé)任編輯:田樹 來(lái)源: 博客
相關(guān)推薦

2009-11-05 11:05:19

WCF服務(wù)合同

2009-11-06 13:23:27

WCF模式

2021-10-24 08:37:18

網(wǎng)絡(luò)監(jiān)控網(wǎng)絡(luò)架構(gòu)網(wǎng)絡(luò)

2022-06-10 08:01:17

ReduxReact

2024-06-24 01:00:00

2024-08-29 09:01:39

2010-08-05 13:44:12

Flex布局

2011-11-24 16:34:39

Java

2013-10-17 09:25:52

2023-11-27 13:42:00

消息隊(duì)列RocketMQ

2022-08-01 07:56:23

React Hook開發(fā)組件

2011-03-16 09:05:53

NATiptables

2012-09-11 09:55:26

編程HTML5編程能力

2014-12-25 09:41:15

Android加載方式

2017-07-06 15:40:19

DevOps核心能力

2019-10-24 07:42:28

Java引用GC

2021-12-22 09:34:01

Golagn配置方式

2009-12-09 11:03:45

安裝Linux

2013-07-29 10:10:40

TCP協(xié)議TCP定時(shí)器TCP

2020-05-07 21:18:30

物聯(lián)網(wǎng)維護(hù)成本IOT
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)