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

WCF繼承實(shí)際應(yīng)用技巧分享

開發(fā) 開發(fā)工具
當(dāng)一個(gè)熟悉了使用C#繼承的開發(fā)人員開始使用WCF繼承進(jìn)行編寫程序時(shí),都會(huì)有哪些不一樣的體驗(yàn)?zāi)兀吭谶@里我們將會(huì)向大家詳細(xì)介紹。

當(dāng)我們?cè)谑褂?a >WCF開發(fā)工具進(jìn)行相應(yīng)功能的開發(fā)時(shí),首先要熟練掌握的當(dāng)然是基于這一工具下的代碼的編寫方式。那么今天我們就先來體驗(yàn)一下WCF繼承的相關(guān)應(yīng)用方式,以此加深我們對(duì)這方面的認(rèn)知程度。

在過去中,我們已經(jīng)習(xí)慣了C#繼承的各個(gè)特性,我們可以按如下的方式定義我們的繼承關(guān)系:

 

  1. [ServiceContract]  
  2. public interface ISimpleCalculator  
  3. {  
  4. //Other Members  
  5. [OperationContract]  
  6. int Add(int arg1, int arg2);  
  7. }   
  8. [ServiceContract]  
  9. public interface IScientificCalculator : ISimpleCalculator  
  10. {  
  11. [OperationContract]  
  12. int Multiply(int arg1, int arg2);  

 

Ok,不要擔(dān)心,在服務(wù)端這樣的特性依然穩(wěn)健地存在著:

 

  1. public class ScientificCalculatorService : IScientificCalculator  
  2. {  
  3. //Other Members   
  4. #region IScientificCalculator Members   
  5. public int Multiply(int arg1, int arg2)  
  6. {  
  7. return arg1 * arg2;  
  8. }   
  9. #endregion   
  10. #region ISimpleCalculator Members   
  11. public int Add(int arg1, int arg2)  
  12. {  
  13. return arg1 + arg2;  
  14. }   
  15. #endregion  

 

但是緊接著,Client端呢?

 

  1. [System.CodeDom.Compiler.GeneratedCodeAttribute
    ("System.ServiceModel", "3.0.0.0")]  
  2. [System.ServiceModel.ServiceContractAttribute(ConfigurationName=
    "ServiceReference.IScientificCalculator")]  
  3. public interface IScientificCalculator {  
  4. //Other Members  
  5. [System.ServiceModel.OperationContractAttribute(Action=
    "http://tempuri.org/ISimpleCalculator/Add"ReplyAction=
    "http://tempuri.org/ISimpleCalculator/AddResponse")]  
  6. int Add(int arg1, int arg2);  
  7. [System.ServiceModel.OperationContractAttribute(Action=
    "http://tempuri.org/IScientificCalculator/Multiply"
    ReplyAction="http://tempuri.org/IScientificCalculator/MultiplyResponse")]  
  8. int Multiply(int arg1, int arg2);  

 

在Reference.cs文件內(nèi),我們只能看到IScientificCalculator 接口的身影,卻找不到ISimpleCalculator的蹤跡。而事實(shí)上我們?cè)诜?wù)端對(duì)這兩個(gè)接口都定義了ServiceContract的Attribute,也許這對(duì)你來說并不重要,或者你不太關(guān)心這些繼承特性所帶來的優(yōu)勢(shì),但是正也是因?yàn)檫@些繼承特性所能帶來的優(yōu)勢(shì)(包括多態(tài)等經(jīng)典的OO特性)我們需要改造這個(gè)Reference.cs以使其適應(yīng)我們“真正的需要”。類似以下的應(yīng)用將會(huì)失?。?/p>

 

  1. static void Main(string[] args)  
  2. {  
  3. ScientificCalculatorClient calculator = new ScientificCalculatorClient();   
  4. UseScientificCalculator(calculator);  
  5. calculator.Close();  
  6. }   
  7. //Will not be supported now  
  8. static void UseSimpleCalculator(ISimpleCalculator calculator)  
  9. {  
  10. Console.WriteLine("Calculator Add : {0}", calculator.Add(5, 4));  
  11. }   
  12. static void UseScientificCalculator(IScientificCalculator calculator)  
  13. {  
  14. Console.WriteLine("Calculator Add : {0}", calculator.Add(5, 4));  
  15. Console.WriteLine("Calculator Multiply : {0}", calculator.Multiply(5, 4));  

 

當(dāng)前的WCF繼承問題就是:#t#

ISimpleCalculator接口在客戶端是不被識(shí)別的。要解除這樣的矛盾,就是要讓客戶端也擁有該接口。

首先我們考慮到我們與Service之間的通信是依賴ServiceContract來描述的,ServiceContract就類似OO中的Interface,一經(jīng)發(fā)布就不可以修改了(盡量!)。我們能做的最好就是能在Client端將這些內(nèi)容重新搭建起來,包括之間的繼承關(guān)系。

在Add ServiceReference之后系統(tǒng)為我們自動(dòng)生成了很多內(nèi)容,找到Reference.cs,這將是我們大刀闊斧的地方……

我們可以看到它里面只實(shí)現(xiàn)了一個(gè)IScientificCalculator接口,這是我們先前就提到過的,我們的系統(tǒng)調(diào)用服務(wù),都是通過從這里獲取它們想要的“服務(wù)端”的一些類去構(gòu)造本地實(shí)例來完成一系列操作的。那么我們現(xiàn)在只需要在這里引入相應(yīng)的接口繼承結(jié)構(gòu)即可……

將原來實(shí)現(xiàn)的唯一接口注釋掉,并添加以下代碼:

 

  1. //[System.CodeDom.Compiler.GeneratedCodeAttribute
    ("System.ServiceModel", "3.0.0.0")]  
  2. //[System.ServiceModel.ServiceContractAttribute(ConfigurationName = 
    "ServiceReference.IScientificCalculator")]  
  3. [ServiceContract]  
  4. public interface ISimpleCalculator  
  5. {  
  6. //Other Members   
  7. // TODO: Add your service operations here  
  8. [OperationContract]  
  9. int Add(int arg1, int arg2);  
  10. }  
  11. //[System.CodeDom.Compiler.GeneratedCodeAttribute
    ("System.ServiceModel", "3.0.0.0")]  
  12. //[System.ServiceModel.ServiceContractAttribute(ConfigurationName =
     
    "ServiceReference.IScientificCalculator")]  
  13. [ServiceContract(ConfigurationName="ServiceReference.
    IScientificCalculatorVolnet"
    )]  
  14. public interface IScientificCalculator : ISimpleCalculator  
  15. {   
  16. [OperationContract]  
  17. int Multiply(int arg1, int arg2);  

 

我們需要using System.ServiceModel之后才可使用以上的WCF繼承代碼,該代碼片斷其實(shí)沒有什么很特別的地方,它與服務(wù)端的接口繼承沒有什么大的出入,唯一需要關(guān)注的則是我黑體標(biāo)注的“ConfigurationName="ServiceReference.IScientificCalculatorVolnet"”,注意,我這里不是在為自己的昵稱做廣告噢,而是以示區(qū)別。

責(zé)任編輯:曹凱 來源: CSDN
相關(guān)推薦

2010-03-01 17:52:03

WCF選擇綁定

2009-12-21 14:49:27

2010-02-22 13:28:05

WCF異步調(diào)用

2010-02-22 15:20:54

WCF WS-Disc

2010-02-22 17:21:02

WCF消息交換

2010-02-25 15:25:19

WCF通道

2010-03-01 15:40:04

WCF實(shí)例停用

2010-03-02 10:50:57

WCF元數(shù)據(jù)交換

2010-02-25 18:04:02

WCF IIS宿主

2010-02-23 13:03:34

WCF序列化

2010-02-26 10:46:12

WCF行為擴(kuò)展

2010-03-01 09:48:23

WCF會(huì)話服務(wù)

2010-02-25 10:52:29

WCF響應(yīng)服務(wù)

2010-02-01 17:09:07

C++鏈表操作

2010-02-24 17:07:26

WCF序列化引擎

2010-02-22 17:58:06

WCF異步上傳

2010-03-03 16:25:41

Python字符串顯示

2010-02-26 14:12:27

WCF元數(shù)據(jù)

2010-02-23 16:46:47

WCF并發(fā)能力

2010-02-22 11:25:50

WCF DateSet
點(diǎn)贊
收藏

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