处理不同异常的不同方面

时间:2014-11-19 03:43:21

标签: c# postsharp onexception

从我所做的阅读中,我预计我将能够创建从OnExceptionAspect继承的不同Aspects,这将允许我在代码中以不同方式处理不同的异常。

为此,我创建了两个Aspect类,如下所示:

[Serializable]
[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true, Inherited = false)]
public class CommonExceptionAspect : OnExceptionAspect
{
    public override void OnException(MethodExecutionArgs args)
    {
        string msg = string.Format("{0} had an error @ {1}: {2}\n{3}",
        args.Method.Name, DateTime.Now,
        args.Exception.Message, args.Exception.StackTrace);

        Trace.WriteLine(msg);

        if (args.Exception.GetType() != typeof (PppGeneralException))
        {
            throw new Exception("There was a problem");
        }
        else
        {
            args.FlowBehavior = FlowBehavior.RethrowException;
        }
    }
}

另一方面:

[Serializable]
[AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Method)]
[MulticastAttributeUsage(MulticastTargets.Method, TargetMemberAttributes = (MulticastAttributes.Public | MulticastAttributes.NonAbstract | MulticastAttributes.Instance | MulticastAttributes.UserGenerated))] 
public class AuthenticationExceptionAspect : OnExceptionAspect
{
    public override void OnException(MethodExecutionArgs args)
    {
        string msg = string.Format("{0} had an error @ {1}: {2}\n{3}",
        args.Method.Name, DateTime.Now,
        args.Exception.Message, args.Exception.StackTrace);

        Trace.WriteLine(msg);

        throw new Exception("You are not authorized!");
    }

    public override Type GetExceptionType(System.Reflection.MethodBase targetMethod)
    {
        return typeof(AuthenticationException);
    }
}

我的来电方法如下:

   public void EstablishConnection(string connectionString, DateTime d1, int connections)
    {
        Thread.Sleep(5000);
        if (connectionString.Length > 0)
        {
            throw new ExternalException("This is my external exception");
        }
    }

    public void StopDatabase(string connectionString)
    {
        Thread.Sleep(5000);

        if (connectionString.Length > 0)
        {
            throw new AuthenticationException("This is my detailed exception");
        }
        else
        {
            throw  new ArgumentException("This is just an argument exception");
        }
    }

我在AssemblyInfo.cs文件中有以下条目:

[assembly: CommonExceptionAspect(AspectPriority = 3, AttributePriority = 1)]
[assembly: CommonExceptionAspect(AttributeExclude = true, AspectPriority = 3, AttributePriority = 0, AttributeTargetMembers = "ConsoleApplication3.ConnectionManager.StopDatabase", AttributeTargetTypeAttributes = MulticastAttributes.Public, AttributeTargetMemberAttributes = MulticastAttributes.Public, AttributeTargetElements = MulticastTargets.Method)]
[assembly: AuthenticationExceptionAspect(AspectPriority = 1)]

我的期望是,当第一个调用方法引发“ExternalException”时,“CommonExceptionAspect”的OnException方法将处理它。当从第二个调用方法引发“AuthenticationException”时,“AuthenticationExceptionAspect”的OnException方法。

但在这两种情况下,调用都会转到“CommonExceptionAspect”。有人可以指出我做错了什么吗?如果这种理解是不正确的,并且如果这种情况完全可以实现。

提前感谢加载。

1 个答案:

答案 0 :(得分:0)

AspectPriority的情况下,数字越大意味着首先应用方面(或更确切地说是转换)。因此,在您的情况下,您首先应用CommonExceptionAspect,无论其他方面如何,此方面都将收到所有异常。

您需要以其他方式设置优先级。

作为旁注 - 多个OnException(或OnMethodBoundary)方面的组合不会创建具有多个catch语句的单个try-catch块,但会创建多个嵌套的try-catch块。因此,行为可能不同。