Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostSharp OnMethodBoundaryAspect OnEntry Not Executing

I am running a .NET 4.0 Web Application (not web site) and PostSharp 1.5. I cannot get the OnEntry override method to execute using the OnMethodBoundaryAspect base class. Here is some relevant code:

public sealed class MonitorAttribute : OnMethodBoundaryAspect {

    public string[] SomeValue { get; protected set; }         

    public MonitorAttribute (params string[] someValue){
        SomeValue = someValue;
    }

    public override void OnEntry(MethodExecutionEventArgs eventArgs){
        // do Something here
        base.OnEntry(eventArgs);
    }

}

public sealed class MyUsageClass : IMyUsageClass {

    [Monitor(new string[]{ 'Test' })
    public void SomeMethod {
        // Do something else in here
    }        

}

Am I missing something? It never hits the OnEntry method. I also tried replacing my PostSharp.dll and PostSharp.Laos.dll dependencies with the new 2.0 version. If it makes any difference MyUsageClass is instantiated by StructureMap.

like image 759
a432511 Avatar asked Jun 16 '11 20:06

a432511


1 Answers

Yes, every dev will need to have PostSharp installed. If you're just using the starter edition then it's all free.

Posting this as an answer to show you the code. My test code

class Program
    {
        [Monitor]
        static void Main(string[] args)
        {

        }
    }

    [Serializable]
    public class MonitorAttribute : OnMethodBoundaryAspect
    {
        public override void OnEntry(MethodExecutionArgs args)
        {
            Console.WriteLine("OnEntry");
        }
    }

The code after compilation

internal class Program
    {
        [CompilerGenerated, DebuggerNonUserCode]
        internal sealed class <>z__Aspects
        {
            internal static MethodBase m1 = MethodBase.GetMethodFromHandle(ldtoken(Main()));
            internal static readonly MonitorAttribute a0 = (MonitorAttribute)<>z__AspectsImplementationDetails.aspects1[0];
        }
        private static void Main(string[] args)
        {
            Program.<>z__Aspects.a0.OnEntry(null);
        }
    }
like image 168
Dustin Davis Avatar answered Oct 11 '22 20:10

Dustin Davis