Search code examples
.netpostsharp

PostSharp OnMethodBoundaryAspect not firing on some asbtract method


I can override some method like GetHashCode or my own abstract class but aspect not firing on SqlConnection.Open and Close

SqlConnection a = new SqlConnection("Password=qwsa;Persist Security Info=True;User ID=sa2;Initial Catalog=mydb;Data Source=.");
a.Open(); // not firing
var zzzz = a.GetHashCode(); //firing
a.Close(); //not firing
Class1 class1 = new Class1(); //firing
var zzzzzzz=class1.testttt(); //not firing

Here is my code

[Serializable]
public class TestOverrideAspect : OnMethodBoundaryAspect
{
    public override void OnExit(MethodExecutionArgs args)
    {
        //DoWORK
    }
}

#if DEBUG
[assembly: PostsharpAspects.TestOverrideAspect (AttributeTargetAssemblies = "*", AttributeTargetTypes = "*", AttributeInheritance=MulticastInheritance.Multicast )]

Solution

  • Generally, applying aspects on external assemblies is a sign of bad use, even though it is possible and in some cases necessary. This is especially true in case of OnMethodBoundaryAspect and abstract method where you want the aspect to actually change all overloads (if you specify the inheritance).

    You should use MethodInterceptionAspect which has different theoretical background and works better in case you need. If you apply such aspect on System.Data.SqlClient.SqlConnection.Open it should work properly and should intercept the abstract method. You just don't have usual advices so you need to implement similar code yourself.

    Please also note that we had a related bug in attribute multicasting, which was recently fixed, so please check that you have the latest version.