PostSharp OnMethodBoundaryAspect OnEntry Not Executing
Asked Answered
B

1

8

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.

Baeyer answered 16/6, 2011 at 20:18 Comment(7)
Use ILSpy (or reflector) and examine your method after compile time. Your target emthod should have a try/catch wrapped around it or at the very least, a call to the aspect's OnEntry method at the very top of the method body.Iridaceous
Very cool. I'm downloading ILSpy right now.... I'll post back shortly. Thanks!Baeyer
Ok. Looking at the method through ILSpy, I do not see a call to OnEntry at the top of the method body.Baeyer
Then i'm not so sure the aspect is getting applied to your method. I would remove all postsharp isntlalations, download latest postsharp, install and rereference them and try again. Do you need 1.5 for a reason (licensing maybe?)Iridaceous
No, I inherited this codebase and PostSharp was being used (apparently not properly) already. Does the development machine need to be running some sort of additional plugin/install of postsharp for it to compile correctly?Baeyer
I could probably switch to 2.0 if necessary. But does that mean every dev's machine needs to be "running" PostSharp? Forgive my ignoranceBaeyer
yes each dev will need a copy installed.Iridaceous
I
2

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);
        }
    }
Iridaceous answered 16/6, 2011 at 21:2 Comment(1)
Ok, I was just poking through the documentation on PostSharps website and it looks like I need to do a full install of PostSharp on my machine. Thanks for all your help!Baeyer

© 2022 - 2024 — McMap. All rights reserved.