Question

I want to intercept a method call before execution using spring.NET. Let's assume the class/method to be intercepted is:

public class Listener
{
    public void Handle()
    {
        // method body
    }
}

This is what I've done (assuming all code is in a namespace called Example):

1.Created the advice:

public class MyAopAdvice : IMethodBeforeAdvice
{
    public void Before(MethodInfo method, object[] args, object target)
    {
        // Advice action
    }
}

2.Updated my spring xml configs:

  <object id="myAopAdvice" type="Example.MyAopAdvice" />

  <object id="listener" type="Spring.Aop.Framework.ProxyFactoryObject">
    <property name="Target">
      <object type="Example.Listener" autowire="autodetect"/>
    </property>
    <property name="InterceptorNames">
      <list>
        <value>myAopAdvice</value>
      </list>
    </property>
  </object>

For some reason my Advice code is not getting hit if I put a breakpoint in it. However, if I add some console logging statements within my advice, it seems they are logged, but not at the appropriate time (i.e., before calling Listener.Handle()).

I'm willing to bet my configs are wrong (for once, I may be missing a way to tell the configs to listen for just the Handle method call and not any other method that Listener may have). Any ideas what's wrong?

Was it helpful?

Solution

Declare your Handle method as virtual:

public virtual void Handle()  // ...

Your class does not implement any interfaces, which spring.net's default aop mechanism uses to create proxies. When spring.net does not find any interfaces to proxy, it looks for virtual methods to create a proxy for a class.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top