我正在開發一個測試程序,幫助我弄清楚如何使用Microsoft.Practices.EnterpriseLibrary.ExceptionHandling框架。該程序定義了幾種自定義異常類型,並將自定義異常處理程序與每種類型關聯在運行時,程序會提示異常拋出的類型的用戶,拋出異常,並使用ExceptionHandling框架呼籲異常類型相應的異常處理程序:爲什麼在調用ExceptionPolicy.HandleException時收到System.InvalidOperationException?
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.Practices.ServiceLocation.ActivationException:激活嘗試獲取類型 ExceptionPolicyImpl,關鍵的「政策」的情況下 出錯--- > Microsoft.Practices.Unity.ResolutionFailedException:分辨率的 依賴失敗,類型= 「Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicyImpl」, 名 = 「政策」。發生異常時:解析時。異常是:InvalidOperationException - 類型ExceptionPolicyImpl有多個長度爲2的構造函數 。無法消除歧義。
我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>
誰能告訴我爲什麼我收到此異常以及如何解決這個問題呢?
嘗試在「HandleException」調用中將「策略」更改爲「策略」。 –