Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a Custom class, InvalidCodeException in Project A

public class InvalidCodeException : Exception
    {
        public InvalidCodeException ()
        {
        }
        public InvalidCodeException (string message)
            : base(message)
        {            
        }

        public InvalidCodeException (string message, Exception innerException)
            : base(message, innerException)
        {

        }
    }

a WCF service in Project B. And a client in Project C. Project A is referenced in Project B and C.

I am throwing InvalidCodeException from Project B and catching in Project C. Problem is that when debuggin, the exception is not catching in

catch (InvalidCodeException ex)
{
  Trace.WriteLine("CustomException");
  throw;
}

but in

catch (Exception ex)
{ throw; }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
207 views
Welcome To Ask or Share your Answers For Others

1 Answer

WCF will not serialize exceptions automatically, for many reasons(e.g. client may be written in some other language than C#, and run on platform that is different from .NET).

However, WCF will serialize some exception information into FaultException objects. This basic information contains exception class name, for example. You may store some additional data inside them if you use generic FaultException type. I.e. FaultException<FaultInfo> where FaultInfo is your class that stores additional exception data. Don't forget to add data contract serialization attributes onto class properties.

Also, you should apply FaultContract attributes on methods that are supposed to throw FaultExceptions of your kind.

[OperationContract]
[FaultContract(typeof(FaultInfo))]
void DoSomething();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...