私のDALモジュールの1つには、次のような形の重複した配管がたくさんあります。
while (retry)
{
...
try
{
...do something
retry = false;
}
catch (SqlException sqlEx)
{
// Retry only if -2 = Connection Time Out or 1205 = Deadlock
if (sqlEx.Number == -2 || sqlEx.Number == 1205)
{
..retry if attempt < max
}
..log and rethrow exception
}
}
最近PostSharpを発見したので、これらの配管コードを属性に置き換えようとしています。
私の当初の計画は次のとおりでした: - OnMethodInvocationAspect を拡張し、メソッド呼び出し中にメソッド呼び出しイベント引数を記憶する - IOnExceptionAspect を実装し、OnException を実装して例外の種類をチェックし、再試行が必要な場合は、元の呼び出しからメソッド呼び出しイベント引数オブジェクトを使用します。
[Serializable]
public sealed class RetryAttribute : OnMethodInvocationAspect, IOnExceptionAspect
{
[NonSerialized]
private MethodInvocationEventArgs m_initialInvocationEventArgs = null;
public override void OnInvocation(MethodInvocationEventArgs eventArgs)
{
if (m_initialInvocationEventArgs == null)
m_initialInvocationEventArgs = eventArgs;
base.OnInvocation(eventArgs);
}
public void OnException(MethodExecutionEventArgs eventArgs)
{
// check if retry is necessary
m_initialInvocationEventArgs.Proceed();
}
}
しかし、IOnExceptionAspect を追加すると、OnInvocation メソッドは起動されなくなりました。
ここで何をする必要があるか知っている人はいますか?それとも、私が使用すべきより適切な側面がありますか?
ありがとう、