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()メソッドを呼び出しました。この方法を使用すると、すべてがうまく機能しました。次に、代わりにExceptionPolicy.HandleException()を使用するようにMainを変更しました。これを行ったとき、私はこの例外を受け取り始めました:
未処理の例外:Microsoft.Practices.ServiceLocation.ActivationException:タイプExceptionPolicyImpl、キー"policy"のインスタンスを取得しようとしたときにアクティブ化エラーが発生しました--->Microsoft.Practices.Unity.ResolutionFailedException:依存関係の解決に失敗しました。タイプ="Microsoft。 Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicyImpl "、name="policy"。例外が発生しました:解決中。例外は次のとおりです。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>
なぜこの例外が発生するのか、問題を解決する方法を教えてもらえますか?