服务抛出的FaultException()异常未被客户端捕获catch(FaultException)

发布于 2024-09-05 23:49:23 字数 1304 浏览 5 评论 0原文

好吧,我知道我在这里遗漏了一些东西。我有以下操作合同:

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 以获取 catch(FaultException exp) 时,会执行相同的 catch 块。我对此不确定,因为我从操作合同中抛出了FaultException。我在这里错过了什么吗?

感谢您的帮助, Ashish

编辑:- 当我更新客户端中的服务引用时,我能够捕获 FaultException 异常。

Ok, I know I am missing something here. I have the following operation contract:

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;
}

And following is taken from the client:-

  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());
  }

In the above case catch(FaultException exp) (the first catch block with ArgumentException in the client code) block does not get executed. However, when I remove ArgumentException to have catch(FaultException exp), the same catch block gets executed. I am not sure about this as I am throwing FaultException from my operation contract. Am I missing anything here.

Appreciate your help,
Ashish

EDIT :- When I updated the service reference in my client, I was able to catch the FaultException<ArgumentException> exception.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡莣 2024-09-12 23:49:23

对于某些任意数据协定类型,请尝试使用 FaultException 。如果您查看代理类中错误的生成代码,我打赌您会发现 ArgumentException 没有按照您期望的方式进行序列化。

Try it with FaultException<DataContract> for some arbitrary data contract type. If you look at the generated code for the fault in the proxy class, I bet you'll see that the ArgumentException is not serializing the way you expect it to.

一影成城 2024-09-12 23:49:23

如果您要派生 Exception 类,请确保您的自定义异常具有序列化构造函数

protected MyCustomException(
          SerializationInfo info,
          StreamingContext context)
            : base(info, context) { }

If you are deriving Exception class, make sure that your custom exception has the serialization constructor

protected MyCustomException(
          SerializationInfo info,
          StreamingContext context)
            : base(info, context) { }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文