为什么我在调用 ExceptionPolicy.HandleException 时出现 System.InvalidOp
本文关键字:System InvalidOp HandleException 调用 ExceptionPolicy 为什么 | 更新日期: 2023-09-27 18:34:28
我正在开发一个测试程序,以帮助我弄清楚如何使用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();
}
}
}
}
在这个程序的第一次迭代中,我使用了 ExceptionManager.Process(( 方法来调用我的 ThrowSomething(( 方法。使用这种方法,一切都很好。然后我修改了 Main 以使用 ExceptionPolicy.HandleException(( 代替。当我这样做时,我开始收到以下异常:
未处理的异常: Microsoft.实践.服务位置.激活异常:激活 尝试获取类型的实例时出错 ExceptionPolicyImpl,关键的"策略"---> Microsoft.实践.统一.决议失败异常:解决 依赖项失败,类型 = "Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicyImpl", 名字 ="政策"。在以下情况下发生异常:解析时。异常是:InvalidOperationException - 类型ExceptionPolicyImpl 有多个 长度为 2 的构造函数。无法消除歧义。
我的应用程序配置文件包含以下内容:
<?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>
谁能告诉我为什么我会收到此异常以及如何解决问题?
好吧,我觉得自己很愚蠢。问题是拼写"策略"是用小写的"p"而不是大写的"P"。更改行
if (ExceptionPolicy.HandleException(ex, "policy", out exToThrow))
自
if (ExceptionPolicy.HandleException(ex, "Policy", out exToThrow))
修复了问题。