none
Using FaultException to transmit an exception from a server to a client RRS feed

  • Question

  • Consider the following scenario:

    // A data contract: [DataContract] public DataServiceException { ... [DataMember] // Some public member declaration. } // An interface: [ServiceContract]
    public interface ISomeInterface { [OperationContract] [FaultContract(typeof(DataServiceException))] void SomeFunc() ; } // A method in a class that implements the interface: void SomeFunc() { DataServiceException Ex = new DataServiceException() ; throw new FaultException<DataServiceException>(Ex, "Some text" ) ; } // Some client side code ISomeInterface Proxy = DuplexChannelFactory<ISomeInterface>.CreateChannel(...) ; try { Proxy.SomeFunc() ; } catch ( FaultException<DataServiceException> E ) { ... } catch ( FaultException E ) { ... } catch ( CommunicationException E) { ... } catch ( Exception E ) { ... }

    (Note I am using a DuplexChannelFactory<> rather than a plain ChannelFactory<>, as ultimately I intend to have some callbacks -- I would hope that this detail is irrelevant to my problem.)

    As things stand, all works as I expect; in the client I can catch the FaultException<DataServiceException>. However, if I change my DataServiceException so that it derives from Exception (directly or indirectly), e.g.

    [DataContract]
    public class DataServiceException : Exception
    {
    [DataMember]
    // Some public member declaration
    }

    Now I no longer catch the FaultException<DataServiceExcpetion>, but catch the CommunicationException instead. (I tried this as my starting point, because in "Programming WCF Services", 3rd Edtion by Juval Lowy, page 262 seems to imply that is a better idea.)

    Should I be able to use Exception derived classes with FaultException<>, and if so can anyone point out what I may be going wrong.

    Thank you in advance,

    Andrew Ch.

    Monday, April 10, 2017 1:24 PM

Answers

  • After checking depth of FaultException, it seems there is some wrong in my first post.

    How did you implement DataServiceException? If you use FaultException<DivideByZeroException> in your service, will it be catch by client?

    If it will, I suggest you try below Exception-derived class.

        [Serializable]
        public class MyCustomException : Exception
        {
            public MyCustomException()
            {
            }
            public MyCustomException(string message) : base(message)
            {
            }
            public MyCustomException(string message, Exception innerException) : base(message, innerException)
            {
            }
            protected MyCustomException(SerializationInfo info, StreamingContext context) : base(info, context)
            {
            }
        }



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, April 12, 2017 7:28 AM

All replies

  • >>Should I be able to use Exception derived classes with FaultException<>

    In my option, you misunderstood “Exception-derived” from "Programming WCF Services". “Exception-derived” class does not mean you define a new class which derived from Exception object. It means FaultException<TDetail> Class which is derived from FaultException->CommunicationException->SystemException.

    Instead of FaultException<DivideByZeroException>, the service could also have thrown
    
    a non-Exception-derived class:
    
    throw new FaultException<double>(number2);

    From “Programming WCF Services” above, FaultException<DivideByZeroException> is FaultException<TDetail> which is Exception-derived class, and FaultException<double> is not.

    In conclusion, your original way is recommended. 



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, April 11, 2017 3:11 AM
  • Hello Edward,

    Thank you very much for taking your time to reply to my query. I think that I understand what you are saying; when "Programming WCF Services" says "... a non-Exception-derived class" it is not using the word "derived" in terms of class inheritance?

    Andrew.

    Tuesday, April 11, 2017 8:09 AM
  • After checking depth of FaultException, it seems there is some wrong in my first post.

    How did you implement DataServiceException? If you use FaultException<DivideByZeroException> in your service, will it be catch by client?

    If it will, I suggest you try below Exception-derived class.

        [Serializable]
        public class MyCustomException : Exception
        {
            public MyCustomException()
            {
            }
            public MyCustomException(string message) : base(message)
            {
            }
            public MyCustomException(string message, Exception innerException) : base(message, innerException)
            {
            }
            protected MyCustomException(SerializationInfo info, StreamingContext context) : base(info, context)
            {
            }
        }



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, April 12, 2017 7:28 AM
  • Hello Edward,

    I apologise for my rudeness in not replying earlier. I have been asked to address a few other issues at work and so have not had time to try out your suggestion. I do hope that I will be able to get back to it soon.

    As ever, thank you very much for taking the time to respond, it is much appreciated.

    Andrew.

    Thursday, April 13, 2017 8:22 AM
  • If you have any update about this issue, please feel free to let us know.

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, April 14, 2017 2:27 AM
  • Hello Edward,

    Once again I do apologise for my long delay. After making the change that you suggested my code behaved as I expected and intended.

    Thank you very much for your help.

    Andrew.

    Friday, April 21, 2017 11:40 AM