问题
I have a simple WCF service that carries out a simple operation:
[OperationContract]
DoSomething (Stuff input);
If an exception occurs inside DoSomething
then a FaultException
will be returned. Given that all the client needs to know is whether something went wrong, would you say that there's no need to define a FaultException
in this scenario?
回答1:
It is always good practice to return a FaultException
, since if you do not, the channel will be faulted and cannot be used again.
The decision what information need to be sent to the client is taken in the configuration (in service behaviour):
<serviceBehaviors>
<behavior name="myName">
<serviceDebug includeExceptionDetailInFaults="true" />
// ....
In fact I always implement IErrorHandler behaviour on the service to catch all exceptions and return FaultException<T>
so that I do not have to do it in all my operations.
来源:https://stackoverflow.com/questions/5514470/when-to-use-wcf-fault-exception