5
從我的理解下面應該正確地從一個WCF REST服務引發自定義錯誤:WCF REST服務的錯誤處理和WebChannelFactory
[DataContract(Namespace = "")]
public class ErrorHandler
{
[DataMember]
public int errorCode { get; set; }
[DataMember]
public string errorMessage { get; set; }
} // End of ErrorHandler
public class Implementation : ISomeInterface
{
public string SomeMethod()
{
throw new WebFaultException<ErrorHandler>(new ErrorHandler()
{
errorCode = 5,
errorMessage = "Something failed"
}, System.Net.HttpStatusCode.BadRequest);
}
}
在提琴手這似乎工作,我得到以下原始數據:
HTTP/1.1 400 Bad Request
Cache-Control: private
Content-Length: 145
Content-Type: application/xml; charset=utf-8
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
Set-Cookie: ASP.NET_SessionId=gwsy212sbjfxdfzslgyrmli1; path=/; HttpOnly
X-Powered-By: ASP.NET
Date: Thu, 03 May 2012 17:49:14 GMT
<ErrorHandler xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><errorCode>5</errorCode><errorMessage>Something failed</errorMessage></ErrorHandler>
但現在我的客戶端,我有以下代碼:
WebChannelFactory<ISomeInterface> client = new WebChannelFactory<ISomeInterface>(new Uri(targetHost));
ISomeInterface someInterface = client.CreateChannel();
try
{
var result = someInterface.SomeMethod();
}
catch(Exception ex)
{
// Try to examine the ErrorHandler to get additional details.
}
現在,當代碼運行時,它遇到了問題,並且帶有'遠程服務器返回意外響應:(400)錯誤請求'消息的System.ServiceModel.ProtocolException
'。看來我無法在這一刻看到ErrorHandler的細節。有沒有人遇到過這個?有什麼方法可以獲得ErrorHander詳細信息?