[英]WCF Client not able to see Custom Fault Exception
正如您可能從問題標題中可以看出,我是WCF的新手。 我已經實現了此Web服務,並且其中一種方法引發了自定義FaultException。
namespace Feed
{
[ServiceContract]
public interface IMyClass
{
[OperationContract]
[FaultContract(typeof(CustomError))]
DataSet MyMethod(int A, int T);
}
[DataContract]
public class CustomError
{
public CustomError(string msg, int code, int id)
{
Id = id;
ErrorMessage = msg;
ErrorCode = code;
}
[DataMember]
[DataMember]
public string ErrorMessage
{
get;
set;
}
[DataMember]
public int ErrorCode
{
get;
set;
}
[DataMember]
public int Id
{
get;
set;
}
}
}
然后,在MyMethod的實現中拋出適當的異常
namespace Feeds
{
public class MyClass : IMyClass
{
DataSet MyMethod(int A, int T)
{
try
{
//todo
DataSet ds = new DataSet();
return ds;
}
catch (Exception ex)
{
throw new FaultException<CustomError>(new CustomError("a", 1, 0));
}
}
}
}
到現在為止還挺好。 現在,我創建了一個使用此Web服務的簡單ASP.NET網站。 我在該網站上為MyClass.svc Web服務添加了WebReference。 問題是,當我嘗試捕獲異常時:catch(FaultException ex){}無法識別CustomError。 我已經完成了以下疑難解答:1.確保我已更新wsdl(做了很多次,嘗試更改其他內容,所有內容都反映在客戶端上)2.嘗試了BasicHttpBinding和wsHttpBinding(不確定是否有所作為) 3.大量搜索Google,並認真遵循MSDN的示例代碼。
還是沒有運氣! 我想念什么嗎? 任何建議將不勝感激。
找出問題所在。
我的ASP.NET網站位於.NET Framework 2.0(這是VS 2010的默認設置)上,並且通過右鍵單擊項目並選擇“添加Web引用。”來添加對Web服務的引用。
但是,我需要做什么,請選擇“添加服務引用..”以將Web服務添加為服務引用。 由於.NET 2.0不支持服務引用,因此我必須將.NET框架升級到3.5,添加服務引用,所有內容都可以正常運行! 這篇文章點燃了我腦海中的燈泡。 感謝大家為此付出的所有努力。
[FaultContract(typeof(CustomError))]
MyMethod();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.