Castle Windsor interceptor not working with method level attribute

I have a lock interceptor that I am trying to apply through attributes. It works great when I apply the Interceptor attribute at the class level, but it doesn't work at all when I apply the method at the method level. What am I doing wrong? I don’t want to intercept every method in the class, but instead mark certain methods using the [Interceptor] attribute. I tried marking my methods as virtual, but this still doesn't work. Here is my code:

This works, and all methods are intercepted:

[Interceptor(typeof(CacheInterceptor))]
public class Foo : IFoo
{
   public int SomeMethod() { }
}

This does NOT work (the attribute is at the method level):

public class Foo : IFoo
{
   [Interceptor(typeof(CacheInterceptor))]
   public int SomeMethod() { }
}

Installer:

public class CacheInterceptorInstaller : IWindsorInstaller
{
    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        container.Register(Component.For<CacheInterceptor>().LifeStyle.Singleton);

        container.Register(Component
            .For<ICacheProvider>()
            .LifeStyle.Singleton
            .ImplementedBy<CacheProvider>());
    }
}

Interceptor:

public class CacheInterceptor : IInterceptor
{
    private readonly ICacheProvider _cacheProvider;

    public CacheInterceptor(ICacheProvider cacheProvider)
    {
        _cacheProvider = cacheProvider;
    }

    public void Intercept(IInvocation invocation)
    {
       // do interception stuff
    }
}

Thanks,

Andy

+4
source share
2

:

[Interceptor(typeof(CacheInterceptor))]
public class Foo : IFoo
{
   [Cache]
   public int SomeMethod() { }
}

Intercept , :

if (invocation.MethodInvocationTarget.IsDefined(typeof(CacheAttribute))) {
  // do interception stuff
}
+7

, , Interceptor . http://docs.castleproject.org/Windsor.Interceptors.ashx:

InterceptorAttribute , , , , , . Windsor , . , .

( )

, -, Castle, IProxyGenerationHook / IInterceptorSelector. : http://docs.castleproject.org/Tools.Use-proxy-generation-hooks-and-interceptor-selectors-for-fine-grained-control.ashx

IInterceptorSelector Interceptor.

+3

All Articles