FaultException <T> () The exception caused by the service is not caught by the catch client (FaultException <T>)

OK, I know something is missing here. I have the following 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;
}

After that it 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 code in client code) is not executed. However, when I remove ArgumentException to have a catch (FaultException exp), the same catch block is executed. I am not sure about this because I am throwing a FaultException from my operation contract. I missed something here.

Appreciate your help, Ashish

EDIT: - When I updated the service link in my client, I managed to catch an exception FaultException<ArgumentException>.

+3
2

FaultException<DataContract> . -, , , ArgumentException , .

+3

Exception, ,

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

Source: https://habr.com/ru/post/1749806/


All Articles