日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

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

新聞中心

這里有您想知道的互聯(lián)網營銷解決方案
WCFopenation實際應用異常解決方案

WCF的實際應用方法多樣化,要想全部掌握是一件非常困難的事情。不過我們可以在不斷的實踐中去積累應用經驗,以幫助我們提高熟練應用程度。在這里就可以先學到一個WCF openation的應技巧。

很多時候我們用到方法的重載,在WCF中也不例外.不過需要加一點東西.我們以正常的方法來寫一個方法的重載,代碼如下:

 
 
 
  1. [ServiceContract]  
  2. public interface ICalculatorContract  
  3. {  
  4. [OperationContract]  
  5. int add(int x, int y);  
  6. [OperationContract]  
  7. double add(double x, double y);  

我把add方法進行了重載.

 
 
 
  1. public class CalculatorService:ICalculatorContract  
  2. {  
  3. #region ICalculatorContract Members  
  4. int ICalculatorContract.add(int x, int y)  
  5. {  
  6. return x + y;   
  7. }  
  8. #endregion  
  9. #region ICalculatorContract Members  
  10. public double add(double x, double y)  
  11. {  
  12. return x + y;   
  13. }  
  14. #endregion  

host 如下:

 
 
 
  1. BasicHttpBinding binding = new BasicHttpBinding();   
  2. Uri baseUri=new Uri ("http://172.28.3.45/CalculatorService");  
  3. ServiceHost host = new ServiceHost(typeof(CalculatorService), baseUri);   
  4. host.AddServiceEndpoint(typeof(ICalculatorContract), 
    binding,string.Empty);  
  5. ServiceMetadataBehavior behavior = host.Description.Behaviors.
    Find();  
  6. if (behavior == null)  
  7. {  
  8. behavior = new ServiceMetadataBehavior();  
  9. behavior.HttpGetEnabled = true;  
  10. behavior.HttpGetUrl = baseUri;  
  11. host.Description.Behaviors.Add(behavior);  
  12. }  
  13. host.Open(); 

這時我們運行host會出現(xiàn)異常:

Cannot have two operations in the same contract with the same name, methods add and add in type CalculatorContract.ICalculatorContract violate this rule. You can change the name of one of the operations by changing the method name or by using the Name property of OperationContractAttribute.

出現(xiàn)這個異常的原因是因為soap message action,不能區(qū)分這兩個方法:所以解決如下:

 
 
 
  1. [ServiceContract]  
  2. public interface ICalculatorContract  
  3. {  
  4. [OperationContract(Name="add1")]  
  5. int add(int x, int y);  
  6. [OperationContract(Name="add2")]  
  7. double add(double x, double y);  

為WCF openation加一個***的name值.這樣不可以soap message區(qū)分這兩個方法了.再次運行host.沒有異常了.

這樣客戶端就可以正常使用add方法.

【編輯推薦】

  1. MSMQ使用WCF正確實現(xiàn)技巧講解
  2. WCF PreSession模式保持調用狀態(tài)
  3. WCF PreCal模式基本代碼示例解析
  4. WCF使用Nhibernate具體操作步驟圖解
  5. WCF枚舉實現(xiàn)技巧總結

當前文章:WCFopenation實際應用異常解決方案
文章鏈接:http://www.dlmjj.cn/article/dpjecjs.html