Entlib 自定义异常处理程序缺少映射

Entlib Custom exception handler missing mappings

我实现了可以正常工作的自定义异常处理程序,但来自 xml 配置策略的映射除外。这些映射适用于标准 Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler

我的实现

[ConfigurationElementType(typeof(CustomHandlerData))]
    public class IdentityFaultContractExceptionHandler : IExceptionHandler
    {
        public IdentityFaultContractExceptionHandler(NameValueCollection attributes)
        {
        }

        public IdentityFaultContractExceptionHandler(Type faultContractType, NameValueCollection attributes)
        {
        }

        public IdentityFaultContractExceptionHandler(Type faultContractType, string exceptionMessage, NameValueCollection attributes)
        {
        }

        public IdentityFaultContractExceptionHandler(IStringResolver exceptionMessageResolver, Type faultContractType, NameValueCollection attributes)
        {
        }

        public Exception HandleException(Exception exception, Guid handlingInstanceId)
        {
            return new Exception();
        }

及部分配置

<add name="All Exceptions" type="System.Exception, mscorlib" postHandlingAction="ThrowNewException">
          <exceptionHandlers>
            <add type="MyClass.IdentityFaultContractExceptionHandler, MyClass" exceptionMessage="An error occurred in the service." faultContractType="MyClass.UnexpectedServerFault, MyClass" name="Fault Contract Exception Handler" >
              <mappings>
                <add source="{Message}" name="Message" />
              </mappings>
            </add>
          </exceptionHandlers>
        </add>

当我删除 mappping 节点服务时,当我添加时,出现错误:无法识别元素 mappings

如果您使用的是 CustomHandlerData 属性,那么您的配置需要使用 XML 属性,然后将其作为 NameValueCollection 传递给自定义处理程序构造函数。如果您想要自定义 XML,那么您将不得不使用 Full Design-time Integration. If you want to go down that road then you should look at the FaultContractExceptionHandlerData 源代码,因为您的代码可能非常相似。