我使用REST從第三方Web服務獲取數據(使用WebHttpBinding
和WebHttpBehavior
)。當我在查詢字符串中傳遞一個無效參數時,服務以500狀態作出響應並描述問題。響應看起來像這樣:當使用WCF服務返回500錯誤時捕獲響應
HTTP/1.1 500 Internal Server Error
Date: Tue, 14 Jun 2011 16:12:48 GMT
... more headers
<Error>Invalid integer value for ID</Error>
我真的很想抓住那個錯誤信息。我的WCF客戶端是這樣的:
public class DataClient : ClientBase<IDataClient>, IDataClient
{
public DataClient (string endpointAddress)
: base(new WebHttpBinding(), new EndpointAddress(endpointAddress))
{
this.Endpoint.Behaviors.Add(new WebHttpBehavior());
}
public string GetData(string id)
{
// This is where the exception is thrown
return base.Channel.GetData(id);
}
}
當我趕上拋出異常,數據已無處可尋。它只是說,「System.ServiceModel.CommunicationException:內部服務器錯誤」。
我試着創建一個自定義行爲並添加了一個消息檢查器,但是我的自定義消息檢查器代碼在引發異常之前沒有得到執行(如果沒有引發異常,它會得到執行)。
您是否在服務web目錄的web.config中添加了'? –
2011-06-14 17:06:40
我只是客戶在使用其他人的服務。我無法控制服務。 – dontangg 2011-06-14 17:15:01
您必須聯繫誰提供此服務,並詢問如何獲取詳細的例外信息。 – 2011-06-14 17:23:26