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

WCF openation實(shí)際應(yīng)用異常解決方案

開發(fā) 開發(fā)工具
在WCF進(jìn)行重載的時(shí)候,可能會(huì)出現(xiàn)一種本文介紹的異常出現(xiàn)。那么如何才能更好的解決問題呢?我們可以在WCF openation中加如一些操作即可。

WCF的實(shí)際應(yīng)用方法多樣化,要想全部掌握是一件非常困難的事情。不過(guò)我們可以在不斷的實(shí)踐中去積累應(yīng)用經(jīng)驗(yàn),以幫助我們提高熟練應(yīng)用程度。在這里就可以先學(xué)到一個(gè)WCF openation的應(yīng)技巧。

很多時(shí)候我們用到方法的重載,在WCF中也不例外.不過(guò)需要加一點(diǎn)東西.我們以正常的方法來(lái)寫一個(gè)方法的重載,代碼如下:

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

我把a(bǔ)dd方法進(jìn)行了重載.

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

這時(shí)我們運(yùn)行host會(huì)出現(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)這個(gè)異常的原因是因?yàn)閟oap message action,不能區(qū)分這兩個(gè)方法:所以解決如下:

  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加一個(gè)***的name值.這樣不可以soap message區(qū)分這兩個(gè)方法了.再次運(yùn)行host.沒有異常了.

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

【編輯推薦】

  1. MSMQ使用WCF正確實(shí)現(xiàn)技巧講解
  2. WCF PreSession模式保持調(diào)用狀態(tài)
  3. WCF PreCal模式基本代碼示例解析
  4. WCF使用Nhibernate具體操作步驟圖解
  5. WCF枚舉實(shí)現(xiàn)技巧總結(jié)
責(zé)任編輯:曹凱 來(lái)源: 博客園
相關(guān)推薦

2010-02-26 15:46:48

Silverlight

2009-11-06 15:25:25

WCF異常

2010-02-25 14:53:44

WCF調(diào)用服務(wù)異常

2010-02-23 14:56:18

WCF Bug

2009-05-22 09:24:00

Blue Coat網(wǎng)絡(luò)優(yōu)化安全

2010-02-22 10:29:11

WCF上傳文件

2010-02-24 09:28:37

WCF安全配置

2010-04-30 17:33:27

Oracle數(shù)據(jù)集成

2017-06-01 11:17:57

Python異常重試解決方案

2018-09-14 16:20:37

2009-12-07 15:50:27

WCF文件

2011-05-05 15:36:25

深信服廣域網(wǎng)加速

2010-03-01 13:06:49

WCF繼承

2010-07-13 16:36:07

SQLServer占內(nèi)

2010-04-28 11:48:13

Oracle MySQ

2010-04-20 11:56:30

Oracle物理結(jié)構(gòu)故

2009-11-05 12:45:25

WCF異常

2009-12-08 15:19:58

WCF大數(shù)據(jù)量

2020-09-04 13:50:35

前端異常監(jiān)控代碼

2009-08-19 16:54:38

綜合布線系統(tǒng)數(shù)據(jù)中心機(jī)柜
點(diǎn)贊
收藏

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