2
votes

I'm trying to use the code from this page, http://docs.castleproject.org/Windsor.Introduction-to-AOP-With-Castle.ashx and register an interceptor in a fluent manner. But I get this error thrown. I've tried Castle Windsor versions from 2.5 to 3.3. So it must be something very basic in how interceptors are set up

Classes

public interface ISomething
{
    Int32 Augment(Int32 input);
    void DoSomething(String input);
    Int32 Property { get; set; }
}

class Something : ISomething
{
    public int Augment(int input) {
        return input + 1;
    }

    public void DoSomething(string input) {
        Console.WriteLine("I'm doing something: " + input);
    }

    public int Property { get; set; }
 }

public class DumpInterceptor : IInterceptor
{
    public void Intercept(IInvocation invocation) {
        Console.WriteLine("DumpInterceptorCalled on method " +
            invocation.Method.Name);
        invocation.Proceed();

        if (invocation.Method.ReturnType == typeof(Int32)) {
            invocation.ReturnValue = (Int32)invocation.ReturnValue + 1;
        }

        Console.WriteLine("DumpInterceptor returnvalue is " +
            (invocation.ReturnValue ?? "NULL"));
    }     
}

Setup

Console.WriteLine("Run 2 - configuration fluent");
using (WindsorContainer container = new WindsorContainer())
{
    container.Register(
        Component.For<IInterceptor>()
        .ImplementedBy<DumpInterceptor>()
        .Named("myinterceptor"));
    container.Register(
        Component.For<ISomething>()
        .ImplementedBy<Something>()
     .Interceptors(InterceptorReference.ForKey("myinterceptor")).Anywhere);


    ISomething something = container.Resolve<ISomething>(); //Offending row

    something.DoSomething("");

    Console.WriteLine("Augment 10 returns " + something.Augment(10));
}

Error

Type 'Castle.Proxies.ISomethingProxy' from assembly'DynamicProxyGenAssembly2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' is attempting to implement an inaccessible interface.

1
Same thing if I add the interceptor with [Interceptor("myinterceptor")]Chris Noring
I just copy-pasted your code into a fresh console app and it works for me. Are you sure this is your code?Yuval Itzchakov
It actually solved itself after I placed each class and interface in its own file. I also removed and readded castle windsor. I think maybe they were internal classes before on the main class, I'm not sure. It works now though... That was a lot of hours wasted :). Thanks for trying though @YuvalItzchakovChris Noring
yes, that was it. You can register and resolve inner classes but you can't add interceptors to them.. Hmm sneaky :)Chris Noring
@Chris, don't hesitate to clean up your code to keep only relevant info, and then post your latest comment as an answer, this really is an important point, which I'd upvotesamy

1 Answers

2
votes

The answer

So I found why this was happening. Appearantly if you create inner classes and interfaces you can register and resolve them but attaching interceptors to them won't work

Example - where the error will be triggered

class Program
{
    public static void Main(String [] args)
    {
        var container = new WindsorContainer();
        container.Register(Component.For<TestInterceptor>().Named("test"));
        container.Register(Component.For<InnerInterface>().ImplementedBy<InnerClass>().Interceptors(InterceptorReference.ForKey("test")).Anywhere);
        // this row below will throw the exception
        var innerClassInstance = container.Resolve<InnerInterface>();
    }

    class InnerClass : InnerInterface  { }

    interface InnerInterface { }

    class TestInterceptor : IInterceptor
    {
        public void Intercept(IInvocation invocation)
        {
            throw new NotImplementedException();
        }
    }
}

Conclusion

So to conclude my intention was not to create inner classes in the first place but rather put together a demo to showcase Castle Windsor. But maybe this can help someone if they run into the same error as me..