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

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

新聞中心

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

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

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

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

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

 
 
 
  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));  

當前的WCF繼承問題就是:#t#

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

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

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

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

將原來實現(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繼承代碼,該代碼片斷其實沒有什么很特別的地方,它與服務端的接口繼承沒有什么大的出入,唯一需要關注的則是我黑體標注的“ConfigurationName="ServiceReference.IScientificCalculatorVolnet"”,注意,我這里不是在為自己的昵稱做廣告噢,而是以示區(qū)別。


本文名稱:WCF繼承實際應用技巧分享
URL地址:http://m.5511xx.com/article/dpjpdci.html