Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems creating a Castle.Windsor interceptor

Ok, I am officially losing my mind over this ...

I am trying to create a Castle.Windsor interceptor, but resolving from the container keeps throwing this exception:

DependencyResolverException: An interceptor registered for 
DI_Test.DatabaseService doesn't implement the IInterceptor interface

As far as I can see I have done everything by the book, and the container contents (in debug-mode) doesn't report any mal-configured services.

Configuration of the container:

public class ControllersInstaller : IWindsorInstaller
{
    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        container
            .Register(Component.For<Runner>())
            .Register(Component.For<IDataDependency>()
                .ImplementedBy<DatabaseService>()
                .LifestyleSingleton()
                .Interceptors(InterceptorReference.ForKey("wait")).Anywhere)
            .Register(Component.For<WaitAndRetryInterceptor>().LifeStyle.Singleton
                .Named("wait"))
            ;
    }
}

My interceptor:

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

My program:

public class Runner
{
    public void Run()
    {
        _dataDependency.GetData();
    }

    public Runner(IDataDependency dataDependency)
    {
        _dataDependency = dataDependency;
    }
    private readonly IDataDependency _dataDependency;
}

public interface IDataDependency
{
    void GetData();
}

public class DatabaseService : IDataDependency
{
    public void GetData()
    {
        throw new NotImplementedException();
    }
}

The program works perfectly without the configuration of the interceptor.

What I cannot figure out is WHY it throws this exception. The interceptor is clearly implementing the IInterceptor interface ... so what is the problem?

Thanks :-)

like image 759
Jesper Lund Stocholm Avatar asked May 12 '26 07:05

Jesper Lund Stocholm


1 Answers

There are two interfaces named IInterceptor

  • Castle.DynamicProxy.IInterceptor
  • Castle.Core.Interceptor.IInterceptor

Not sure what is the difference between them, but to get it working you must use the first one.

like image 172
Jan Muncinsky Avatar answered May 14 '26 20:05

Jan Muncinsky



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!