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

成都創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設,海門企業(yè)網(wǎng)站建設,海門品牌網(wǎng)站建設,網(wǎng)站定制,海門網(wǎng)站建設報價,網(wǎng)絡營銷,網(wǎng)絡優(yōu)化,海門網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學習、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網(wǎng)站。
C++與C#均支持操作的重載,但在WCF的編程模型中,卻并不支持這種技術(shù)。坦白說,在WCF的編程模型,對于面向?qū)ο蟮闹С侄际潜容^弱的,包括后面要介紹的繼承體系與多態(tài),都存在許多問題。因此,在服務端我們不能定義這樣的WCF服務契約:
- [ServiceContract]
- interface ICalculator
- {
- [OperationContract]
- int Add(int arg1,int arg2);
- [OperationContract]
- double Add(double arg1,double arg2);
- }
雖然在編譯時能夠通過,然而一旦在裝載宿主時,就會拋出InvalidOperationException異常。以ICalculator契約為例,WCF會認為是零個操作。
解決的辦法是利用OperationContract特性的Name屬性,例如:
- [ServiceContract]
- interface ICalculator
- {
- [OperationContract(Name = "AddInt")]
- int Add(int arg1,int arg2);
- [OperationContract(Name = "AddDouble")]
- double Add(double arg1,double arg2);
- }
不過采用這種方式,存在的問題是生成的代理會將Name屬性指定的名稱作為代理操作的方法名。這對于編程者而言,并非好的方式。所幸我們可以手動對生成的代理進行修改,將它修改為與WCF服務契約一致的操作名。由于,此時通過Name指定了操作的別名,因此,避免了裝載宿主拋出的異常。
契約的繼承
即使父接口標記了[ServiceContract],子接口仍然需要標記[ServiceContract],因為ServiceContractAttribute是不可繼承的。服務類對服務契約的實現(xiàn),與傳統(tǒng)的C#編程沒有什么區(qū)別。例如:
- [ServiceContract]
- interface ISimpleCalculator
- {
- [OperationContract]
- int Add(int arg1,int arg2);
- }
- [ServiceContract]
- interface IScientificCalculator : ISimpleCalculator
- {
- [OperationContract]
- int Multiply(int arg1,int arg2);
- }
- class MyCalculator : IScientificCalculator
- {
- public int Add(int arg1,int arg2) { return arg1 + arg2;
- }
- public int Multiply(int arg1,int arg2) { return arg1 * arg2;
- }
- }
公開終結(jié)點的時候,可以對***層的契約接口公開一個單獨的終結(jié)點:
- < service name=”MyCalculator”> < endpoint> < addressaddress=
”http://localhost:8001/MyCalculator/”> < bindingbinding=
”basicHttpBinding”> < contractcontract=” IScientificCalculator”>
< /endpoint> < /service>
客戶端在導入如上的WCF服務契約時,會取消服務契約的繼承層級,并利用OperationContract特性中的Action與ReplyAction屬性,保留原來定義每個操作的契約名。但為了使客戶端編程能夠與服務編程保持一致,***是恢復客戶端的契約層級。方法并無什么太玄妙的地方,無非就是根據(jù)服務契約層級對客戶端契約進行手工修改。修改后的客戶端契約及其代理的定義如下:
- [ServiceContract]
- public interface ISimpleCalculator {
- [OperationContract]
- int Add(int arg1,int arg2);
- }
- public partial class SimpleCalculatorClient : ClientBase
< ISimpleCalculator>, ISimpleCalculator- {
- public int Add(int arg1,int arg2)
- {
- return Channel.Add(arg1,arg2);
- } //Rest of the proxy }
- [ServiceContract]
- public interface IScientificCalculator : ISimpleCalculator {
- [OperationContract]
- int Multiply(int arg1,int arg2);
- }
- public partial class ScientificCalculatorClient : ClientBase
< IScientificCalculator>,IScientificCalculator {- public int Add(int arg1,int arg2) {
- return Channel.Add(arg1,arg2); }
- public int Multiply(int arg1,int arg2) {
- return Channel.Multiply(arg1,arg2); }
- //Rest of the proxy }
在書中還提出了所謂的代理鏈(Proxy Chaining)技術(shù),實質(zhì)上就是使得分別實現(xiàn)不同層級接口的代理類形成一個IS-A的繼承關系。如上的定義,就可以使ScientificCalculatorClient繼承自SimpleCalculatorClient,而不是繼承ClientBase< IScientificCalculator>:
- public partial class SimpleCalculatorClient :
ClientBase< IScientificCalculator>, ISimpleCalculator {- public int Add(int arg1,int arg2) {
- return Channel.Add(arg1,arg2);
- } //Rest of the proxy }
- public class ScientificCalculatorClient : SimpleCalculatorClient,
IScientificCalculator {- public int Multiply(int arg1,int arg2) {
- return Channel.Multiply(arg1,arg2); } //Rest of the proxy }
只有這樣,如下代碼才是正確的:
- SimpleCalculatorClient proxy1 = new SimpleCalculatorClient( );
- SimpleCalculatorClient proxy2 = new ScientificCalculatorClient( );
- ScientificCalculatorClient proxy3 = new ScientificCalculatorClient( );
以上就是對WCF服務契約的相關介紹。
【編輯推薦】
- WCF限流操作實際設置方式揭秘
- WCF實例停用基本應用技巧分享
- WCF分布操作應對特定操作情況
- WCF死鎖三種不同方式介紹
- WCF回調(diào)契約如何進行正確定義
文章題目:WCF服務契約基本應用技巧解讀
當前網(wǎng)址:http://fisionsoft.com.cn/article/dpcccjs.html


咨詢
建站咨詢
