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

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
WCF繼承實(shí)際應(yīng)用技巧分享

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

目前創(chuàng)新互聯(lián)建站已為上1000家的企業(yè)提供了網(wǎng)站建設(shè)、域名、虛擬空間、網(wǎng)站改版維護(hù)、企業(yè)網(wǎng)站設(shè)計(jì)、駐馬店網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。

在過(guò)去中,我們已經(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ì)你來(lái)說(shuō)并不重要,或者你不太關(guān)心這些繼承特性所帶來(lái)的優(yōu)勢(shì),但是正也是因?yàn)檫@些繼承特性所能帶來(lái)的優(yōu)勢(shì)(包括多態(tài)等經(jīng)典的OO特性)我們需要改造這個(gè)Reference.cs以使其適應(yīng)我們“真正的需要”。類(lèi)似以下的應(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繼承問(wèn)題就是:#t#

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

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

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

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

將原來(lái)實(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í)沒(méi)有什么很特別的地方,它與服務(wù)端的接口繼承沒(méi)有什么大的出入,唯一需要關(guān)注的則是我黑體標(biāo)注的“ConfigurationName="ServiceReference.IScientificCalculatorVolnet"”,注意,我這里不是在為自己的昵稱(chēng)做廣告噢,而是以示區(qū)別。


分享標(biāo)題:WCF繼承實(shí)際應(yīng)用技巧分享
網(wǎng)頁(yè)地址:http://www.5511xx.com/article/dpjpdci.html