我有兴趣为我的WCF应用程序实现适当的异常处理。在谷歌快速搭车后,我遇到了两种解决方案。处理WCF异常/故障以及FaultException和IErrorHandler
- 为客户提供
FaultExceptions
。 - 使用
IErrorHandler
的一般异常处理。
我没发现什么,虽然是结合了这两种方法的实际例子 - 如果这甚至认为是很好的做法。
我的想法:
投掷FaultExceptions用自己的详细类型
throw new FaultException<StackOverflowFault>(
new StackOverflowFault(StackOverflowFaultCode.Rules,
"This question is not considered as constructive."));
与IErrorHandler
捕获未处理的异常,并记录它们。基于异常的类型
public bool HandleError(Exception ex) {
try {
SomeLogger.Error(ex.Message, ex.Source);
return true;
} catch (Exception) {
// Well, there was an exception handling the
// exception :(
// -> Drop the exception and throw a new one
throw new Exception("Unknown exception occured.");
}
}
......并提供故障(不是一切为了客户的眼睛!)
public void ProvideFault(Exception error, MessageVersion version, ref Message fault){
if(error is FaultException) {
fault = Message.CreateMessage(version, ((FaultException<StackOverflowFault>)error).Reason)
} else if(error is SqlException) {
// What would Jon Skeet do?
}
}
我的问题
这是否被认为是好的做法?并且:如果我已经在适用于客户端的应用程序中抛出FaultException - 是否有意义让它们由IErrorHandler
(自动执行)处理?
+1在您的问题中引用Skeet先生! – STLDeveloper