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

WCF服務契約基本應用技巧解讀

開發(fā) 開發(fā)工具
我們今天將會通過不同的代碼解讀來為大家詳細介紹一下WCF服務契約的分解方式以及相關設計方法,希望能給大家?guī)硇椭?/div>

我們在應用WCF服務契約的時候,需要掌握一些應用技巧,才能幫助我們輕松的應用這一功能來完成各種功能需求。在這里我們就一起來看看WCF服務契約的分解與設計方法,以方便大家理解。

C++與C#均支持操作的重載,但在WCF的編程模型中,卻并不支持這種技術。坦白說,在WCF的編程模型,對于面向對象的支持都是比較弱的,包括后面要介紹的繼承體系與多態(tài),都存在許多問題。因此,在服務端我們不能定義這樣的WCF服務契約:

  1. [ServiceContract]   
  2. interface ICalculator   
  3. {   
  4. [OperationContract]   
  5. int Add(int arg1,int arg2);   
  6. [OperationContract]   
  7. double Add(double arg1,double arg2);   

雖然在編譯時能夠通過,然而一旦在裝載宿主時,就會拋出InvalidOperationException異常。以ICalculator契約為例,WCF會認為是零個操作。

解決的辦法是利用OperationContract特性的Name屬性,例如:

  1. [ServiceContract]   
  2. interface ICalculator   
  3. {   
  4. [OperationContract(Name = "AddInt")]   
  5. int Add(int arg1,int arg2);   
  6. [OperationContract(Name = "AddDouble")]   
  7. double Add(double arg1,double arg2);   

不過采用這種方式,存在的問題是生成的代理會將Name屬性指定的名稱作為代理操作的方法名。這對于編程者而言,并非好的方式。所幸我們可以手動對生成的代理進行修改,將它修改為與WCF服務契約一致的操作名。由于,此時通過Name指定了操作的別名,因此,避免了裝載宿主拋出的異常。

契約的繼承

即使父接口標記了[ServiceContract],子接口仍然需要標記[ServiceContract],因為ServiceContractAttribute是不可繼承的。服務類對服務契約的實現(xiàn),與傳統(tǒng)的C#編程沒有什么區(qū)別。例如:

  1. [ServiceContract]   
  2. interface ISimpleCalculator   
  3. {   
  4. [OperationContract]   
  5. int Add(int arg1,int arg2);   
  6. }   
  7. [ServiceContract]   
  8. interface IScientificCalculator : ISimpleCalculator   
  9. {   
  10. [OperationContract]   
  11. int Multiply(int arg1,int arg2);   
  12. }   
  13. class MyCalculator : IScientificCalculator   
  14. {   
  15. public int Add(int arg1,int arg2) { return arg1 + arg2;   
  16. }   
  17. public int Multiply(int arg1,int arg2) { return arg1 * arg2;   
  18. }   

公開終結點的時候,可以對***層的契約接口公開一個單獨的終結點:

  1. < service name=”MyCalculator”> < endpoint> < addressaddress=
    ”http://localhost:8001/MyCalculator/”
    > < bindingbinding=
    ”basicHttpBinding”
    > < contractcontract=” IScientificCalculator”>
     < /endpoint> < /service>  

客戶端在導入如上的WCF服務契約時,會取消服務契約的繼承層級,并利用OperationContract特性中的Action與ReplyAction屬性,保留原來定義每個操作的契約名。但為了使客戶端編程能夠與服務編程保持一致,***是恢復客戶端的契約層級。方法并無什么太玄妙的地方,無非就是根據(jù)服務契約層級對客戶端契約進行手工修改。修改后的客戶端契約及其代理的定義如下:

  1. [ServiceContract]   
  2. public interface ISimpleCalculator {   
  3. [OperationContract]   
  4. int Add(int arg1,int arg2);   
  5. }   
  6. public partial class SimpleCalculatorClient : ClientBase
    < ISimpleCalculator>, ISimpleCalculator   
  7. {   
  8. public int Add(int arg1,int arg2)   
  9. {   
  10. return Channel.Add(arg1,arg2);   
  11. } //Rest of the proxy }   
  12. [ServiceContract]   
  13. public interface IScientificCalculator : ISimpleCalculator {   
  14. [OperationContract]   
  15. int Multiply(int arg1,int arg2);   
  16. }   
  17. public partial class ScientificCalculatorClient : ClientBase
    < IScientificCalculator>,IScientificCalculator {   
  18. public int Add(int arg1,int arg2) {   
  19. return Channel.Add(arg1,arg2); }   
  20. public int Multiply(int arg1,int arg2) {   
  21. return Channel.Multiply(arg1,arg2); }   
  22. //Rest of the proxy } 

在書中還提出了所謂的代理鏈(Proxy Chaining)技術,實質上就是使得分別實現(xiàn)不同層級接口的代理類形成一個IS-A的繼承關系。如上的定義,就可以使ScientificCalculatorClient繼承自SimpleCalculatorClient,而不是繼承ClientBase< IScientificCalculator>:

  1. public partial class SimpleCalculatorClient : 
    ClientBase
    < IScientificCalculator>, ISimpleCalculator {   
  2. public int Add(int arg1,int arg2) {   
  3. return Channel.Add(arg1,arg2);   
  4. } //Rest of the proxy }   
  5. public class ScientificCalculatorClient : SimpleCalculatorClient, 
    IScientificCalculator {   
  6. public int Multiply(int arg1,int arg2) {   
  7. return Channel.Multiply(arg1,arg2); } //Rest of the proxy } 

只有這樣,如下代碼才是正確的:

  1. SimpleCalculatorClient proxy1 = new SimpleCalculatorClient( );   
  2. SimpleCalculatorClient proxy2 = new ScientificCalculatorClient( );   
  3. ScientificCalculatorClient proxy3 = new ScientificCalculatorClient( ); 

以上就是對WCF服務契約的相關介紹。

【編輯推薦】

  1. WCF限流操作實際設置方式揭秘
  2. WCF實例停用基本應用技巧分享
  3. WCF分布操作應對特定操作情況
  4. WCF死鎖三種不同方式介紹
  5. WCF回調(diào)契約如何進行正確定義
責任編輯:曹凱 來源: IT168
相關推薦

2010-03-01 18:11:40

WCF數(shù)據(jù)契約變更

2010-02-25 10:52:29

WCF響應服務

2010-03-01 09:48:23

WCF會話服務

2010-02-26 13:40:28

WCF消息頭

2010-02-25 18:04:02

WCF IIS宿主

2010-03-01 15:40:04

WCF實例停用

2010-03-01 11:24:31

WCF面向服務

2010-02-23 15:58:57

WCF Session

2010-02-22 13:56:35

WCF服務契約

2010-02-24 16:58:14

WCF Session

2010-03-03 14:30:05

Python set類

2009-12-22 10:16:54

WCF服務狀態(tài)

2009-11-06 15:02:47

WCF契約查詢

2013-12-12 16:10:21

Lua腳本語言

2010-02-06 16:16:01

C++冒泡排序

2010-03-03 15:17:46

Python調(diào)用MyS

2010-03-03 13:32:08

Python壓縮文件

2010-02-25 16:45:13

WCF應用技巧

2010-03-03 13:22:08

Python正則表達式

2009-12-22 09:11:31

WCF雙向通信
點贊
收藏

51CTO技術棧公眾號