c# - 创建 CaSTLe.Windsor 拦截器的问题

标签 c# .net dependency-injection castle-windsor interceptor

好吧,我正式失去理智了......

我正在尝试创建一个 CaSTLe.Windsor 拦截器,但从容器中解析一直抛出此异常:

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

据我所知,我已经按照书中的规定完成了所有操作,并且容器内容(在 Debug模式下)没有报告任何配置错误的服务。

容器的配置:

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"))
            ;
    }
}

我的拦截器:

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

我的程序:

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();
    }
}

程序在没有配置拦截器的情况下运行完美。

我不明白为什么会抛出这个异常。拦截器明明是在实现 IInterceptor 接口(interface)……所以有什么问题?

谢谢:-)

最佳答案

有两个名为IInterceptor的接口(interface)

  • CaSTLe.DynamicProxy.IInterceptor
  • 城堡.Core.Interceptor.IInterceptor

不确定它们之间有什么区别,但要使其正常工作,您必须使用第一个。

关于c# - 创建 CaSTLe.Windsor 拦截器的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48340873/

相关文章:

c# - ASP.NET Core 服务器端验证失败导致 Microsoft.AspNetCore.Mvc.SerializableError

c# - 在同一装配空间中动态生成模块

c# - 有没有办法在 ConfigureServices() 中访问强类型设置?

design-patterns - 如何向一个 5 岁的 child 解释依赖注入(inject)?

C# xml : How to save nested rows

c# - 使用 httpwebrequest/httpwebresponse 从服务器到客户端的 Docx 文件传输

c# - 在 C# 中将 HTML 写入 WebBrowser 的正确方法是什么?

c# - 在 WPF 中,PreviewTextInput 是否总是只给出一个字符?

c# - 错误还是我做错了什么?用户控件绘画

c# - Entity Framework : Injecting dependencies into entities when other options are problematic