Search code examples
c#.netcastle-windsoraopcastle-dynamicproxy

Castle DynamicProxy Interface Proxy Generation


I have a WindsorContainer.

I have a ILazyComponentLoader (if it matters) and an Interface (ISomething) with an Interceptor attribute on in.

[Interceptor(typeof(DynamicImplementationInterceptor)]
public interface ISomething

I want Windsor to use ProxyGenerator.CreateInterfaceProxyWithoutTarget when resolving the interface via container.Resolve<ISomething>() so that my DynamicImplementationInterceptor can implement all behavior on demand, externally.

I can't seem to find this exact scenario in the documentation...when I register ISomething by using Component.For<ISomething>()...., how do I specify I want this behavior? (presently I get an error about the type being abstract or interface, so it can't be instantiated...)

Thanks!


Solution

  • Windsor will automatically omit the target when you register a component with an interceptor and no implementation, e.g.:

    var container = new WindsorContainer();
    container.Register(Component.For<DynamicImplementationInterceptor>());
    container.Register(Component.For<ISomething>()
        .Interceptors(InterceptorReference.ForType<DynamicImplementationInterceptor>()).First);
    

    No need for any Interceptor attribute