C# 为什么调用ExceptionPolicy.HandleException时会得到System.InvalidOperationException?

C# 为什么调用ExceptionPolicy.HandleException时会得到System.InvalidOperationException?,c#,exception,exception-handling,enterprise-library,C#,Exception,Exception Handling,Enterprise Library,我正在编写一个测试程序,帮助我了解如何使用Microsoft.Practices.EnterpriseLibrary.ExceptionHandling框架。程序定义了几种自定义异常类型,并将自定义异常处理程序与每种类型相关联。在运行时,程序会提示用户输入要引发的异常类型,引发异常,并使用ExceptionHandling框架为异常类型调用适当的异常处理程序: using System; using System.Collections.Specialized; using Microsoft.

我正在编写一个测试程序,帮助我了解如何使用Microsoft.Practices.EnterpriseLibrary.ExceptionHandling框架。程序定义了几种自定义异常类型,并将自定义异常处理程序与每种类型相关联。在运行时,程序会提示用户输入要引发的异常类型,引发异常,并使用ExceptionHandling框架为异常类型调用适当的异常处理程序:

using System;
using System.Collections.Specialized;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration;

namespace ConsoleApplication1
{
    public class AException  : Exception  { public AException(string message)  : base(message) { } }
    public class BException  : Exception  { public BException(string message)  : base(message) { } }
    public class BBException : BException { public BBException(string message) : base(message) { } }

    public class WrapperException : Exception
    {
        public WrapperException(Exception innerException)
            : base("Wrapped exception: [" + innerException.Message + "]", innerException) { }
    }

    public class MyExceptionHandler<T> : IExceptionHandler
    {
        protected NameValueCollection Ignore { get; set; }
        public MyExceptionHandler(NameValueCollection ignore)
        {
            Ignore = ignore;
        }

        #region IExceptionHandler Members

        public virtual Exception HandleException(Exception exception, Guid handlingInstanceId)
        {
            if (exception is T)
            {
                Console.WriteLine("Exception Handled:");
                Console.WriteLine("  Expected Type : [{0}]", typeof(T).ToString());
                Console.WriteLine("  Actual Type   : [{0}]", exception.GetType().ToString());
                Console.WriteLine("  Message       : [{0}]", exception.Message);
                Console.WriteLine();
            }
            else
            {
                Console.WriteLine("Unexpected Exception Type: [{0}]", exception.GetType().ToString());
            }
            return exception;
        }

        #endregion
    }

    [ConfigurationElementType(typeof(CustomHandlerData))]
    public class AExceptionHandler : MyExceptionHandler<AException>
    {
        public AExceptionHandler(NameValueCollection ignore) : base(ignore) { }
    }

    [ConfigurationElementType(typeof(CustomHandlerData))]
    public class BExceptionHandler : MyExceptionHandler<BException>
    {
        public BExceptionHandler(NameValueCollection ignore) : base(ignore) { }
    }

    [ConfigurationElementType(typeof(CustomHandlerData))]
    public class ExceptionHandler : MyExceptionHandler<Exception>
    {
        public ExceptionHandler(NameValueCollection ignore) : base(ignore) { }

        public override Exception HandleException(Exception exception, Guid handlingInstanceId)
        {
            var wrapper = new WrapperException(exception);
            return base.HandleException(wrapper, handlingInstanceId);
        }
    }


    class Program
    {
        static void ThrowSomething()
        {
            Console.Write("Enter the exception type: ");
            var x = Console.ReadLine();
            if (x.Equals("a"))
            {
                throw new AException(x);
            }
            else if (x.Equals("b"))
            {
                throw new BException(x);
            }
            else if (x.Equals("bb"))
            {
                throw new BBException(x);
            }
            else
            {
                throw new Exception(x);
            }
        }

        static void Main(string[] args)
        {
            ExceptionManager xm = EnterpriseLibraryContainer.Current.GetInstance<ExceptionManager>();
            while (true)
            {
                //xm.Process(ThrowSomething, "Policy");
                try
                {
                    ThrowSomething();
                }
                catch(Exception ex)
                {
                    Exception exToThrow = null;
                    if (ExceptionPolicy.HandleException(ex, "policy", out exToThrow))
                    {
                        if (exToThrow == null)
                        {
                            throw;
                        }
                        else
                        {
                            throw exToThrow;
                        }
                    }
                }
                Console.WriteLine();
            }
        }
    }
}
使用系统;
使用System.Collections.Specialized;
使用Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
使用Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
使用Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration;
命名空间控制台应用程序1
{
公共类AEException:异常{public AEException(字符串消息):基(消息){}
公共类BeException:异常{public BeException(字符串消息):基(消息){}
公共类BBException:BeException{public BBException(字符串消息):基(消息){}
公共类包装器异常:异常
{
公共包装器异常(异常innerException)
:base(“包装异常:[“+innerException.Message+“]”,innerException){}
}
公共类MyExceptionHandler:IEExceptionHandler
{
受保护的NameValueCollection忽略{get;set;}
公共MyExceptionHandler(NameValueCollection忽略)
{
忽略=忽略;
}
#区域IEExceptionHandler成员
公共虚拟异常HandleException(异常异常,Guid handlingInstanceId)
{
如果(例外情况为T)
{
WriteLine(“异常处理:”);
WriteLine(“预期类型:[{0}]”,typeof(T).ToString();
WriteLine(“实际类型:[{0}]”,exception.GetType().ToString());
WriteLine(“Message:[{0}]”,exception.Message);
Console.WriteLine();
}
其他的
{
WriteLine(“意外异常类型:[{0}]”,Exception.GetType().ToString());
}
返回异常;
}
#端区
}
[ConfigurationElementType(typeof(CustomHandlerData))]
公共类AEExceptionHandler:MyExceptionHandler
{
公共AEExceptionHandler(NameValueCollection忽略):基(忽略){}
}
[ConfigurationElementType(typeof(CustomHandlerData))]
公共类BeExceptionHandler:MyExceptionHandler
{
公共BeExceptionHandler(NameValueCollection忽略):基(忽略){}
}
[ConfigurationElementType(typeof(CustomHandlerData))]
公共类ExceptionHandler:MyExceptionHandler
{
PublicExceptionHandler(NameValueCollection忽略):基(忽略){}
公共重写异常HandleException(异常异常,Guid handlingInstanceId)
{
var wrapper=newwrapperexception(异常);
返回base.HandleException(包装器,handlingInstanceId);
}
}
班级计划
{
静态空隙通过某物()
{
编写(“输入异常类型:”);
var x=Console.ReadLine();
如果(x等于(“a”))
{
抛出新的AEException(x);
}
如果(x等于(“b”))
{
抛出新的异常(x);
}
如果(x等于(“bb”))
{
抛出新异常(x);
}
其他的
{
抛出新异常(x);
}
}
静态void Main(字符串[]参数)
{
ExceptionManager xm=EnterpriseLibraryContainer.Current.GetInstance();
while(true)
{
//xm.过程(通过某种方式,“策略”);
尝试
{
Throuwsomething();
}
捕获(例外情况除外)
{
异常抛出=null;
if(例外策略.处理例外(例如,“策略”,out))
{
if(throw==null)
{
投掷;
}
其他的
{
投掷,投掷;
}
}
}
Console.WriteLine();
}
}
}
}
在这个程序的第一次迭代中,我使用ExceptionManager.Process()方法调用ThrowSomething()方法。使用这种方法,一切都很顺利。然后我修改了Main,改为使用ExceptionPolicy.HandleException()。当我这样做的时候,我开始得到这个异常:

未处理的异常: Microsoft.Practices.ServiceLocation.ActivationException:激活 尝试获取类型的实例时出错 例外PolicyImpl,关键“策略”--> Microsoft.Practices.Unity.ResolutionFailedException:解决 依赖项失败,类型为= “Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicyImpl”, 名称 =“政策”。解析时发生异常。异常为:InvalidOperationException-类型ExceptionPolicyImpl具有多个 长度为2的构造函数。无法消除歧义

My App.config文件包含以下内容:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
    </configSections>
    <exceptionHandling>
        <exceptionPolicies>
            <add name="Policy">
                <exceptionTypes>
                    <add name="AException" type="ConsoleApplication1.AException, ConsoleApplication1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
                        postHandlingAction="None">
                        <exceptionHandlers>
                            <add type="ConsoleApplication1.AExceptionHandler, ConsoleApplication1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
                                name="AExceptionHandler" />
                        </exceptionHandlers>
                    </add>
                    <add name="BException" type="ConsoleApplication1.BException, ConsoleApplication1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
                        postHandlingAction="None">
                        <exceptionHandlers>
                            <add type="ConsoleApplication1.BExceptionHandler, ConsoleApplication1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
                                name="BExceptionHandler" />
                        </exceptionHandlers>
                    </add>
                    <add name="All Other Exceptions" type="System.Exception, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
                        postHandlingAction="NotifyRethrow">
                        <exceptionHandlers>
                            <add type="ConsoleApplication1.ExceptionHandler, ConsoleApplication1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
                                name="ExceptionHandler" />
                        </exceptionHandlers>
                    </add>
                </exceptionTypes>
            </add>
        </exceptionPolicies>
    </exceptionHandling>
</configuration>

if (ExceptionPolicy.HandleException(ex, "policy", out exToThrow))
if (ExceptionPolicy.HandleException(ex, "Policy", out exToThrow))