2011-06-30 52 views

回答

2

首先,您需要啓用WCF服務來返回詳細的錯誤信息。這是默認情況下,出於安全原因(你不想告訴你的攻擊者在你的錯誤信息中的系統的所有細節...)

爲此,你需要創建一個新的或修改現有的與<ServiceDebug>行爲服務行爲:

<behaviors> 
    <serviceBehaviors> 
     <behavior name="ServiceWithDetailedErrors"> 
      <serviceDebug includeExceptionDetailInFaults="true" /> 
     </behavior> 
    </serviceBehaviors> 
</behaviors> 

其次,你需要改變你的<service>標籤引用這個新的服務行爲:

<service name="YourNamespace.YourServiceClassName" 
     behaviorConfiguration="ServiceWithDetailedErrors"> 
    ...... 
</service> 

;第三:您需要調整您的SL解決方案來看看錯誤的細節你現在回來了。

最後一點:雖然此設置在開發和測試中非常有用,但您應爲應爲將這些錯誤詳細信息關閉以進行生產 - 出於安全原因,請參閱上文。

0

除了Marc提到的事情之外,您還需要切換到HTTP客戶端堆棧以避免可怕的通用「未找到」錯誤。

bool registerResult = WebRequest.RegisterPrefix("http://", WebRequestCreator.ClientHttp); 
0

如果您傳遞錯誤的客戶端,您可以使用一個故障合同:

添加這個屬性對您的服務合同:

[OperationContract] 
    [FaultContract(typeof(MyCustomException))] 
    void MyServiceMethod(); 

爲「MyCustomException」創建類含確切地說,你希望傳遞給客戶端的信息(在這種情況下,異常來自exception.ToString())的完整細節。

然後在您的服務方法的實現添加一個try/catch圍繞代碼:

public void MyServiceMethod() 
{ 
     try 
     { 
     // Your code here 
     } 
     catch(Exception e) 
     { 
     MyCustomException exception= new MyCustomException(e.ToString()); 
       throw new FaultException<MyCustomException>(exception); 
     } 
} 

在客戶端,你可以把一個try/catch(FaultException異常e)和顯示你喜歡的細節。

try 
     { 
      // your call here 
     } 
     catch (FaultException<MyCustomException> faultException) 
     { 
      // general message displayed to user here 
      MessageBox.Show((faultException.Detail as MyCustomException).Details); 
     } 
     catch (Exception) 
     { 
      // display generic message to the user here 
      MessageBox.Show("There was a problem connecting to the server"); 
     } 
相關問題