简体   繁体   中英

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 :-)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM