다음을 통해 공유


WCF and Fault

Fault is generally used as a way for webservices to report rich error information to clients who perform webservice call (think .Net exception). WCF has a rich support for webservice to provide fault information.

1) Throw typical exception

2) Throw FaultException

3) Throw FaultException with strongly-typed detail

Note: In either case, this is considered application fault and the client channel state will not be affected and continue to function. There, however, are infrastructure faults such as security that could fault the channel.

1) Service Implementation throws typical exception (not of FaultException types). Such as Exception, ArgumentNullException etc.

[ServiceContract]

public interface IEchoService

{

    [OperationContract]

    string Echo(string msg);

}

public class EchoService : IEchoService

{

    public string Echo(string msg)

    {

        throw new Exception("Echo fault!");

    }

}

WCF service will simply wrap the exception with a Fault message and return to a client. The client will receive a generic fault (FaultException thrown) containing a generic message.

System.ServiceModel.FaultException: The server was unable to process the request due to an internal error. For more information about the error, either turn on IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute or from the <serviceDebug> configuration behavior) on the server in order to send the exception information back to the client, or turn on tracing as per the Microsoft .NET Framework 3.0 SDK documentation and inspect the server trace logs.

If webservice would like to include the exception information into the fault, it may turn on the IncludeExceptionDetailInFaults setting (off by default).

[ServiceBehavior(IncludeExceptionDetailInFaults = true)]

public class EchoService : IEchoService

{

    public string Echo(string msg)

    {

        throw new Exception("Echo fault!");

    }

}

WCF service will wrap the exception with a Fault message including all the details and return to a client. The client will receive a fault (FaultException<ExceptionDetail> thrown) containing a detail message.

System.ServiceModel.FaultException< System.ServiceModel.ExceptionDetail>: Echo fault!

 

2) Service Implementation throws Exception of FaultException type.

WCF provides FaultException classes allowing application to define faultcode, reason, etc. The application may construct and throw the FaultException explicitly as follow.

[ServiceContract]

public interface IEchoService

{

    [OperationContract]

    string Echo(string msg);

}

public class EchoService : IEchoService

{

    public string Echo(string msg)

    {

        throw new FaultException("Echo fault!");

    }

}

WCF service will simply serialize the FaultException as a Fault message and return to a client. All data in the FaultException will be send to client regardless of IncludeExceptionDetailInFaults setting. The client will receive a fault (FaultException thrown) with data defined by the services.

System.ServiceModel.FaultException: Echo fault!

 

3) Service Implementation throws FaultException type with strongly-typed detail

In this case, WCF provides a FaultException<T> class. This allows application to define a strong typed detail with the FaultException. The client would be able to catch FaultException<T> explicitly. The application must decorate the operation with [FaultContract] to assist serialization process.

[DataContract]

public class MyCustomDetail

{

    [DataMember]

    public string ID;

    public MyCustomDetail(string id) { ID = id; }

}

 

[ServiceContract]

public interface IEchoService

{

    [OperationContract]

    [FaultContract(typeof(MyCustomDetail))]

    string Echo(string msg);

}

public class EchoService : IEchoService

{

    public string Echo(string msg)

    {

        throw new FaultException<MyCustomDetail>(new MyCustomDetail(1));

    }

}

The client will receive a fault (FaultException thrown) with strongly-typed detail.

System.ServiceModel.FaultException< MyCustomDetail>: The detail contains the strong-typed detail (MyCustomDetail)

Comments