[英]Autofac trigger lifetimescope creation
Is there any way to cause the creation of a new lifetimescope
when resolving a component? 解决组件时,有什么方法可以导致创建新的lifetimescope
镜吗?
Meaning something like 意思像
container.registerType().As().TriggersNewScope() container.registerType(),作为()。TriggersNewScope()
Is there any other way of doing that? 还有其他方法吗? Other than taking dependency on ILifetimeScope
and to directly resolve through it? 除了依赖ILifetimeScope
并通过它直接解决之外?
If there isn't, is there a way to abstract the ILifetimeScope
? 如果没有,是否有一种抽象ILifetimeScope
? I don't want that part of the application to reference Autofac.. 我不希望应用程序的那一部分引用Autofac。
Thanks 谢谢
You need to register your reused service as InstancePerOwned . 您需要将重用的服务注册为InstancePerOwned 。
The remaining unsolved part is avoiding reference to Autofac, here is a possible workaround: 其余未解决的部分是避免引用Autofac,这是一种可能的解决方法:
Define and register open-generic CustomOwned implementation in Autofac aware composition root: 在可识别Autofac的合成根目录中定义并注册开放的CustomOwned实现:
class CustomOwned<T> : IOwned<T>, IDisposable { public T Value { get { return _owned.Value; } } public CustomOwned(Autofac.Owned<T> owned) { _owned = owned; } // IDisposable implementation and whatever additional stuff }
Use IOwned instead of Owned in your types. 在您的类型中使用IOwned而不是Owned。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.