dependency-injection - CaSTLe Windsor 中的 Controller 未发布依赖项?

标签 dependency-injection castle-windsor

我有一个依赖项(例如 IServiceRetriever ),我的 Controller 调用它又调用另一个依赖项。我可以验证我的 Controller 在“_container.Release(controller) ”调用期间被释放,但我的依赖项没有被释放。我知道这一点,因为如果我再次调用 Controller ,此依赖项的公共(public)属性会不断添加项目(该列表是在构造函数中创建的,因此如果创建一个新实例,它将是一个新列表)。

我将我的 Controller 注册为 LifetimeTransient,并且我没有与我的依赖性相关的生活方式:

container.Register(Classes.FromThisAssembly().BasedOn<IController>().LifestyleTransient());

container.Register(Classes.FromAssemblyContaining<IServiceRetriever>()
                .Where(x => x.IsPublic)
                .WithServiceDefaultInterfaces()); //dependency that calls IQueryBuilder

container.Register(Component.For<IQueryBuilder>().ImplementedBy<QueryBuilder>()); // not being released

我以前从未见过这种情况,我假设如果我释放了 Controller ,Windsor 将遍历依赖关系图并处理它找到的所有内容,但事实并非如此。我在这里缺少什么?

最佳答案

I have never seen this before, I was under the assumption that if I released the controller, Windsor would walk the dependency graph and dispose everything it finds, this is not the case.

如果您的依赖项使用单例生命周期,则此假设是错误的。 CaSTLe Windsor中的默认生命周期是单例,您已经以默认生命周期注册了IServiceRetriever和IQueryBuilder。在您处置容器之前,单例对象不会被释放。如果您在没有对象持有对它的引用时释放它,您可能需要稍后创建另一个对象,并且它不再是单例。

关于dependency-injection - CaSTLe Windsor 中的 Controller 未发布依赖项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17330372/

相关文章:

java - 为 spring ConfigurationProperties 子类使用构造函数注入(inject)

dependency-injection - DI - 找不到 EventBus 的提供程序

caSTLe-windsor - Circular 'interfaces' 依赖项和 CaSTLe-Windsor

c# - 如何使用 CaSTLe Windsor 注册通用装饰器?

.net - NHibernate 和 Windsor 程序集冲突

symfony - 使用 symfony DI 延迟加载依赖项

asp.net - 如何在 ASP.NET Web 服务中使用依赖注入(inject)和存储库模式?

dependency-injection - 温莎城堡 build

.net - 温莎容器 : Registering things in Code vs Xml

c# - Autofac 通用装饰器正在复制组件