我正在使用基于Guy Burstein的博客文章here的企业库来实现异常屏蔽。我的实现与他的基本相同,但是我有几个问题:
1:异常屏蔽的要点是不传递错误的确切细节,那么为什么映射指定原封不动地传递原始异常的消息。提供不太具体的信息的明智方式是什么?
2:回传的faultexception包含来自原始异常的消息,因此无论我设法在错误约定中包装或返回什么,都可以获得有关faultexception的详细信息。有没有办法将信息移回到一般的“内部错误”消息。
请注意,如果我不使用[ExceptionShielding]
和[FaultContract]
属性,那么服务将返回标准的“服务器由于内部错误而无法处理请求”消息。
发布于 2011-03-24 17:31:30
如果您知道您自己的应用程序异常不会泄漏敏感信息,则可以处理这些特定的异常并映射message属性。对于其他异常,您可以跳过映射消息。此外,您还可以在错误合约本身上指定exceptionMessage:
<add type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
postHandlingAction="ThrowNewException" name="Exception">
<exceptionHandlers>
<add exceptionMessage="Oops! A System Error Occurred in the Service Layer." faultContractType="MyTypes.Exceptions.ServiceFault, MyTypes.Exceptions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WCF.FaultContractExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WCF, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
name="Default Fault Contract Handler">
</add>
exceptionMessage
将在FaultException的Message属性上填充。如果您不映射细节,那么这对您来说可能就足够了。
如果您确实想要在您的错误类中填充值,那么您可以分两步完成:
创建一个处理程序来处理您感兴趣的异常,并清理message.
使用Guy Burstein's WCF Exception Handling with Exception Handling Application Block Integration示例,这将设置ServiceFault的MessageText (其余部分基于该示例)。
因此,您的服务将如下所示:
public int CreateOrder(string currency, double amount)
{
try
{
throw new InvalidOperationException("Cannot call this operation!");
}
catch (Exception e)
{
// This sanitizes the message and throws a new Exception
Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicy.
HandleException(e, "SanitizeAndThrow");
}
return 0;
}
然后,在配置中,您将创建一个SanitizeAndThrow处理程序来清理消息:
<add name="SanitizeAndThrow">
<exceptionTypes>
<add type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
postHandlingAction="ThrowNewException" name="Exception">
<exceptionHandlers>
<add exceptionMessage="This is a sanitized message."
replaceExceptionType="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ReplaceHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling"
name="Replace Handler" />
</exceptionHandlers>
</add>
</exceptionTypes>
</add>
然后您可以使用您的异常屏蔽来创建FaultException:
<add name="WCF Exception Shielding">
<exceptionTypes>
<add type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" postHandlingAction="ThrowNewException" name="Exception">
<exceptionHandlers>
<add exceptionMessage="Oops!"
type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WCF.FaultContractExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WCF"
name="DefaultFaultContract Handler"
faultContractType="Bursteg.Samples.WCFIntegration.ServiceContracts.ServiceFault, Bursteg.Samples.WCFIntegration.ServiceContracts">
<mappings>
<add name="Id" source="{Guid}"/>
<add name="MessageText" source="{Message}"/>
</mappings>
</add>
</exceptionHandlers>
</add>
关于此示例的一些注意事项:
Exception
,服务代码没有NotifyRethrow逻辑,等等。Detail
有一种更简单的方法来清理故障异常的(例如,在异常屏蔽中链接处理程序)?
https://stackoverflow.com/questions/5419166
复制相似问题