0

I'm new here and hope my first question does not confuse anyone.

I am using Castle Windsor and Castle DynamicProxy together, in order to integrate AOP with IOC, so that I can implement a logging service (and auditing, etc.) with will intercept all services in my project.

I'm trying to proxy the resolved instance to get this done but with no luck. Because the proxy returned will have the properties of the resolved instance set to null. Following is the debugging info for reference (the PROBLEMATIC property is TimeService).

the resolved instance before proxied

The resolved instance before proxied.

the proxied object

The proxied object

Did I miss something or did i understand Castle DynamicProxy in a wrong way? I did search for any solutions but had no luck.

Any answers is highly appreciated. Thanks in advance.

Sky Feng
  • 3
  • 3

1 Answers1

1

You should use Windsor's built-in support for AOP during registration, not overriding WindsorContainer.Resolve otherwise you'll also run into other problems around releasing components and container lifetimes:

container.Register(
    Component.For<ICalcService>()
        .Interceptors(InterceptorReference.ForType<ReturnDefaultInterceptor>()).Last,
    Component.For<ReturnDefaultInterceptor>()
);

See the docs for more info: https://github.com/castleproject/Windsor/blob/master/docs/registering-interceptors-and-proxyoptions.md

Jonathon Rossi
  • 4,149
  • 1
  • 22
  • 32
  • Thanks very much for your answer, this works for me.One more question, seems this solution requires interceptor configuration for each component (either in code or configuration file), is there a way that you can specify some sort of global interceptors which automatically applies to all the components? – Sky Feng Jul 10 '17 at 06:00
  • @SkyFeng if you are not using the convention driven registration (e.g Classes.FromAssemblyContaining()), then hook container. ComponentRegistered and add to ComponentModel.Interceptors. – Jonathon Rossi Jul 10 '17 at 07:24
  • Got it. Thanks very much. @jonathon-rossi – Sky Feng Jul 10 '17 at 07:59
  • @SkyFeng if you find you want to do more than just add an interceptor you might like to separate the logic into a Windsor facility (for example Windsor's LoggingFacility) or implement the simple IContributeComponentModelConstruction interface. – Jonathon Rossi Jul 10 '17 at 09:02
  • I really appreciate your kind suggestion and it will definitely help my study. I'm new to Castle and there's still lots of stuff to learn (as Castle provides plenty of useful tools). Thanks again. – Sky Feng Jul 10 '17 at 16:17