Castle Windsor 3.2 Interceptor Example

Interceptors are a way of creating a “proxy” class around your types allowing you to manipulate or log the interactions. In this small example i will show you a very simple interceptor. Lets get into the code.

Lets start by creating a very simple interface.

    public interface Itestclass
    {
        void SayHello();
    }

This will be used to create our simple type that we cant castle to handle. So now lets implement it;

    public class testclass : Itestclass
    {

        public void SayHello()
        {
            MessageBox.Show("Hello");
        }
    }

This testclass will just throw a Message Box, its not much but should give you the idea of how the interceptors work.

Now lets setup windsor and register our types.

  IWindsorContainer container = new WindsorContainer();
  container.Register(Component.For<Itestclass>().ImplementedBy<testclass>());

Now you would typically resolve your type like this.

Itestclass t = container.Resolve<Itestclass>();
t.SayHello();

You should see a simple message box being displayed. at this point.

Now lets look at our interceptor which implements the IInterceptor interface.

  public class MyIntercept : IInterceptor {

        #region IInterceptor Members

        public void Intercept(IInvocation invocation) {
            MessageBox.Show(">> intercepted in <<");
            invocation.Proceed();
            MessageBox.Show(">> intercepted out <<");
        }

        #endregion
    }

Its pretty simple and it implements a single method where IInvocation is the MethodInfo data being passed in. I think that you can see what will happen when the Intercept method is called. The invocation.Proceed() call just instructs the interceptor to perform the operation which its wrapping.

So now lets add our intercepter references which requires altering the Windsor component registration shown earlier.

this is the Interceptor registration

 ProxyGenerator _generator = new ProxyGenerator(new PersistentProxyBuilder());
            MyIntercept _myinterceptor = new MyIntercept();
            var proxy = _generator.CreateClassProxy<testclass>(_myinterceptor);

            container.Register(Component.For<IInterceptor>()
                .ImplementedBy<MyIntercept>().Named("myinterceptor"));

This is the Component registration with the interceptor added.

       container.Register(Component.For<Itestclass>()
                                        .ImplementedBy<testclass>()
.Interceptors(InterceptorReference.ForKey("myinterceptor")).Anywhere);     

Notice that we named the interceptor when we registered the interceptor component, we called it “myinterceptor”. Because we did this we can now specify it in the interceptors reference using the ForKey(“”) method. This basically links the type resolution to the interceptor.

So now if we call this, we will get our message box wrapped with 2 other message boxes.

Itestclass t = container.Resolve<Itestclass>();
t.SayHello();

All code together.

    public class maintest {
        public void InterceptorTest() {

            IWindsorContainer container = new WindsorContainer();
            ProxyGenerator _generator = new ProxyGenerator(new PersistentProxyBuilder());
            MyIntercept _myinterceptor = new MyIntercept();
            var proxy = _generator.CreateClassProxy<testclass>(_myinterceptor);

            container.Register(Component.For<IInterceptor>()
                .ImplementedBy<MyIntercept>().Named("myinterceptor"));

            container.Register(Component.For<Itestclass>()
                                        .Named("testing")
                                        .ImplementedBy<testclass>()
                                        .Interceptors(InterceptorReference.ForKey("myinterceptor")).Anywhere);
            
            Itestclass t = container.Resolve<Itestclass>("testing");
           
            t.SayHello();

        }
    }

    public class MyIntercept : IInterceptor {

        #region IInterceptor Members

        public void Intercept(IInvocation invocation) {
            MessageBox.Show(">> intercepted in <<");
            invocation.Proceed();
            MessageBox.Show(">> intercepted out <<");
        }

        #endregion
    }

    public interface Itestclass
    {
        void SayHello();
    }

    

    public class testclass : Itestclass
    {

        public void SayHello()
        {
            MessageBox.Show("Hello");
        }
    }

Hope this makes sense !!

64 Comments

  1. Eureka Pet Lover – At a lot less than 9 extra pounds, the Eureka Pet Fan Mighty Mite is among the
    lighter vacuums we checked out in our ideal.

  2. Bramah manufactures locks for domestic and commercial use.
    • Don’t forget to check its compatibility with
    your building structure and the ability of the system to
    deliver a good sound quality which depends on the structure
    of your home. Services include lock changes, lock change,
    lock replacement, lock rekeying, security safes, grilles and gates and gates.

    my page: רואה כאן עכשיו

  3. They can also provide you the total security systems installation for your residential or commercial purposes.
    • Don’t forget to check its compatibility with your
    building structure and the ability of the system
    to deliver a good sound quality which depends on the structure of your home.
    Only the pins and tumblers of the locks are replaced, thereby
    invalidating existing keys.

    Also visit my homepage … סורגים בראשון לציון

  4. Bramah manufactures locks for domestic and commercial use.
    Here are several things you are able to do to prevent these types
    of situations:. Services include lock changes, lock change, lock replacement, lock
    rekeying, security safes, grilles and gates and gates.

    Look at my web site; צפה בקליפ

  5. In the end of the thinking mind brings with it?
    But the language of desire free sometimes you guys will start
    you thinking about” How To Improve and Save My Marriage Relationship? It really expands your market and the language of desire free shops in the next time oral sex can leave a sweet tasting treat.

  6. Wonderful goods from you, man. I’ve understand your stuff previous to and you are just
    too magnificent. I actually like what you have acquired here, really like what you’re saying and the
    way in which you say it. You make it enjoyable and you still take care of
    to keep it smart. I can’t wait to read far more from you. This is really a
    terrific web site.

    Feel free to surf to my web site … הסרת תוצאות חיפוש

  7. With havin so much content do you ever run into any problems
    of plagorism or copyright infringement? My site has a lot of unique
    content I’ve either authored myself or outsourced but it looks like a lot of
    it is popping it up all over the internet without my permission. Do
    you know any solutions to help reduce content from being stolen?
    I’d really appreciate it.

  8. As a matter of fact, you could acquire phytoceramides nearly throughout the globe, even if you are from Australia, Norway, or Argentina.

  9. This improves the resistance of your skin by eliminating
    the dreadful indicators of growing old and also their terrible impacts of the skin completely.

  10. As the piston transfers down, it will certainly at that point push the coating out
    of the liquid electrical outlet to stream by means of the tip.

  11. With any luck, this helps you as well as some of the highly recommended remedies aids you obtain your video
    recording to used on your Chromebook.

    my blog post … commercial vacuum cleaner (Marcella)

  12. FreeSmith Video Gamer wraps standard media gamer needs, however
    definitely delivers little bit of making that attract attention from the pack.

    Feel free to surf to my page: vacuum cleaner dyson – Darnell

  13. Our company may have just invested $100 on the grill, however our company conveniently destroyed $ONE HUNDRED in meat
    product considering that the grill really did not prepare equally.

  14. With the frontal grille took out, you will definitely have the ability to unscrew the limit that holds the ventilator cutter in position.

    Feel free to surf to my page best for your pet (Bette)

  15. After the 2nd as well as third collection of CD titles
    were actually launched, the complete variety of titles in the end of the year hit around one hundred.

    Feel free to visit my web site: compact vacuum cleaner; Lindsay,

  16. Residential dehumidifier capability and also performance are actually measured with 80F as well as 60
    % family member moisture air entering their gain.

    Look into my homepage; quality vacuum; Alexander,

  17. Actual ceramic pots and pans could handle temps of nearly
    2000º Fahrenheit, which would certainly liquefy your other metal as well as non-stick
    kitchenware.

    Here is my webpage – best vacuum cleaner for carpet and hardwood (Ashley)

  18. Wir haben schon bei den letzten beiden Competition-Ausgaben mit Bitcoin experimentiert und es zum Beispiel für die Abstimmung
    und als Preisgeld eingesetzt.

  19. You’re so interesting! I do not believe I’ve read something like that before.

    So good to discover someone with original thoughts on this subject.
    Really.. thanks for starting this up. This web site is one thing that is required on the
    internet, someone with a bit of originality!

    Here is my site … dog adoption

  20. Hi there! Do you know if they make any plugins to assist with SEO?

    I’m trying to get my blog to rank for some targeted keywords
    but I’m not seeing very good gains. If you know of any please share.
    Thanks!

  21. After all, you cannot inhale useful components from aromatic plants without vaporizing them.

    The parts of the iolite can be easily replaced and removed for purpose of
    repair or cleaning. Once these pollutants are allows to enter the lungs they have the potential to spread and eventually produce harm to other
    vital organs in the body.

Comments are closed.