日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

這里有您想知道的互聯(lián)網營銷解決方案
WCF服務契約基本應用技巧解讀

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

成都創(chuàng)新互聯(lián)公司是一家集網站建設,海門企業(yè)網站建設,海門品牌網站建設,網站定制,海門網站建設報價,網絡營銷,網絡優(yōu)化,海門網站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強企業(yè)競爭力。可充分滿足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學習、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網站。

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屬性,保留原來定義每個操作的契約名。但為了使客戶端編程能夠與服務編程保持一致,***是恢復客戶端的契約層級。方法并無什么太玄妙的地方,無非就是根據服務契約層級對客戶端契約進行手工修改。修改后的客戶端契約及其代理的定義如下:

 
 
 
  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回調契約如何進行正確定義

網頁名稱:WCF服務契約基本應用技巧解讀
標題鏈接:http://www.5511xx.com/article/dpcccjs.html