[英]client will not catch generic FaultException< T >, only FaultException
[英]FaultException<T>() exception thrown by the service is not caught by the client catch(FaultException<T>)
好的,我知道我在這里遺漏了一些東西。 我有以下操作合同:
public double DivideByZero(int x, int y)
{
if (y == 0)
{
throw new FaultException<ArgumentException>
(new ArgumentException("Just some dummy exception")
,new FaultReason("some very bogus reason"), new FaultCode("007"));
}
return x / y;
}
以下內容取自客戶: -
Console.WriteLine("Enter the x value");
string x = Console.ReadLine();
Console.WriteLine("Enter the Y value");
string y = Console.ReadLine();
try
{
double val = client.DivideByZero(Convert.ToInt32(x), Convert.ToInt32(y));
Console.WriteLine("The result is " + val.ToString());
}
catch(FaultException<ArgumentException> exp)
{
Console.WriteLine("An ArgumentException was thrown by the service "+ exp.ToString());
}
catch (Exception exp)
{
Console.WriteLine(exp.ToString());
}
在上面的例子中,catch(FaultException exp)(客戶端代碼中帶有ArgumentException的第一個catch塊)塊沒有被執行。 但是,當我刪除ArgumentException以捕獲(FaultException exp)時,會執行相同的catch塊。 我不確定這一點,因為我從我的操作合同中拋出FaultException。 我在這里遺漏了什么。
感謝你的幫助,Ashish
編輯: - 當我在客戶端更新服務引用時,我能夠捕獲FaultException<ArgumentException>
異常。
對於某些任意數據協定類型,請使用FaultException<DataContract>
進行嘗試。 如果你看一下代理類中生成的錯誤代碼,我敢打賭你會發現ArgumentException
沒有按照你期望的方式序列化。
如果要派生Exception類,請確保自定義異常具有序列化構造函數
protected MyCustomException(
SerializationInfo info,
StreamingContext context)
: base(info, context) { }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.