Interceptors in Windsor are implemented by our DynamicProxy library, which
works by creating a class (e.g. BarProxy) at runtime that inherits your
class (e.g. Bar) and overrides its virtual methods, you can see this in the
debugger or if you print out `bar.GetType().FullName`. When the overridden
method is called it delegates off to your interceptor which must call
Proceed otherwise the base method (i.e. Bar::DoSomething) isn't called.
Below is a really simplified idea of what the runtime generated proxy class
looks like:
public class BarProxy : Bar
{
public override void DoSomething()
{
interceptor.Intercept(invocation);
}
}
It should now be clear the reason the interceptor isn't called for both
Foo::DoSomething and Bar::DoSomething.
On Wed, Jan 21, 2015 at 6:59 PM, 'veihan' via Castle Project Users <
[email protected]> wrote:
> Hello
>
> Probably this is a duplicate... but I will describe my problem anyway.
>
> Lets say I have classes:
>
> public abstract class Foo
> {
> public virtual void DoSomething()
> {
> Console.WriteLine("Doing something base");
> }
> }
>
> public class Bar : Foo
> {
> public override void DoSomething()
> {
> Console.WriteLine("Doing something");
> base.DoSomething();
> }
> }
>
> and interceptor:
>
> public class MyAspect : IInterceptor
> {
> public void Intercept(IInvocation invocation)
> {
> Console.WriteLine("Method is intercepted...");
> invocation.Proceed();
> }
> }
>
> public class MyInterceptorSelector : IModelInterceptorsSelector
> {
> public bool HasInterceptors(ComponentModel model)
> {
> return typeof(MyAspect) != model.Implementation &&
> model.Implementation.Namespace.StartsWith("Dp2");
> }
>
> public InterceptorReference[] SelectInterceptors(ComponentModel
> model, InterceptorReference[] interceptors)
> {
> var interceptorss = interceptors.ToList();
> interceptorss.Add(InterceptorReference.ForType<MyAspect>());
> return interceptorss.ToArray();
> }
> }
>
> and registration:
>
> public class Bootstrapper
> {
> public Bootstrapper(IWindsorContainer container)
> {
> Container = container;
> }
>
> public IWindsorContainer Container { get; private set; }
>
> public void Run()
> {
> Container.Register(Component.For<MyAspect>());
> Container.Kernel.ProxyFactory.AddInterceptorSelector(new
> MyInterceptorSelector());
> Container.Register(Component.For<Foo>().ImplementedBy<Bar>());
> }
> }
>
> in test:
>
> [TestFixture]
> public class UnitTest1
> {
> [Test]
> public void TestMethod1()
> {
> var bts = new Bootstrapper(new WindsorContainer());
> bts.Run();
>
> var bar = bts.Container.Resolve<Foo>();
> bar.DoSomething();
> }
> }
>
> output is:
>
> Method is intercepted...
> Doing something
> Doing something base
>
> ergo interceptor hasn't been called on base method execution.
>
> Is there a way to make this work?
>
> --
> You received this message because you are subscribed to the Google Groups
> "Castle Project Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected]
> .
> Visit this group at http://groups.google.com/group/castle-project-users.
> For more options, visit https://groups.google.com/d/optout.
>
--
Jono
--
You received this message because you are subscribed to the Google Groups
"Castle Project Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/castle-project-users.
For more options, visit https://groups.google.com/d/optout.