成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

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

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

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

在過去中,我們已經(jīng)習(xí)慣了C#繼承的各個特性,我們可以按如下的方式定義我們的繼承關(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的蹤跡。而事實上我們在服務(wù)端對這兩個接口都定義了ServiceContract的Attribute,也許這對你來說并不重要,或者你不太關(guān)心這些繼承特性所帶來的優(yōu)勢,但是正也是因為這些繼承特性所能帶來的優(yōu)勢(包括多態(tài)等經(jīng)典的OO特性)我們需要改造這個Reference.cs以使其適應(yīng)我們“真正的需要”。類似以下的應(yīng)用將會失敗:

 

  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接口在客戶端是不被識別的。要解除這樣的矛盾,就是要讓客戶端也擁有該接口。

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

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

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

將原來實現(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繼承代碼,該代碼片斷其實沒有什么很特別的地方,它與服務(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-25 15:25:19

WCF通道

2010-02-22 15:20:54

WCF WS-Disc

2010-02-22 17:21:02

WCF消息交換

2010-02-25 10:52:29

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

2010-02-23 13:03:34

WCF序列化

2010-02-26 10:46:12

WCF行為擴展

2010-03-01 09:48:23

WCF會話服務(wù)

2010-03-01 15:40:04

WCF實例停用

2010-03-02 10:50:57

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

2010-02-25 18:04:02

WCF IIS宿主

2010-02-01 17:09:07

C++鏈表操作

2010-02-24 17:07:26

WCF序列化引擎

2010-03-03 16:25:41

Python字符串顯示

2010-02-22 17:58:06

WCF異步上傳

2010-02-22 11:25:50

WCF DateSet

2010-02-26 10:56:06

WCF Stream

2010-03-02 17:35:20

WCF服務(wù)加載
點贊
收藏

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

主站蜘蛛池模板: 日本a∨精品中文字幕在线 亚洲91视频 | 日本一区视频在线观看 | 一级高清免费毛片 | www.亚洲 | 日韩电影一区二区三区 | 99国内精品久久久久久久 | 日韩免费一区 | 国产精品免费一区二区三区 | 一本一道久久a久久精品综合蜜臀 | 欧美一区二区三区视频 | 欧美日韩国产欧美 | 国产精品久久久久久久久久三级 | 亚洲国产精品99久久久久久久久 | 久久久久久国产精品 | 一区二区福利视频 | 欧美一区二区三区 | 99免费看 | 精品国产乱码久久久久久牛牛 | 天天爽天天干 | 玖玖玖在线观看 | 日韩毛片 | 麻豆视频在线免费看 | 国产成人jvid在线播放 | 亚洲欧美一区二区三区国产精品 | 亚洲欧美中文日韩在线v日本 | 欧产日产国产精品国产 | 欧美成人免费在线视频 | 欧美日韩一区二区三区在线观看 | 精品国产高清一区二区三区 | 亚洲精品福利视频 | 久久精品久久久 | 男女羞羞视频在线观看 | 在线观看国产三级 | 久久精品一区二区三区四区 | 久久久久久av | 日韩精品一区二区三区中文字幕 | 黄色成人亚洲 | 中文字幕视频在线观看 | 欧美国产亚洲一区二区 | 日本在线看 | 天天爽一爽 |