简体   繁体   English

Autofac触发Lifetimescope的创建

[英]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 谢谢

There's not really a "component triggers lifetime scope creation" thing as such. 确实没有这样的“组件触发生命周期范围创建”的事情。 However, you may be interested in Owned<T> . 但是,您可能对Owned<T>感兴趣。 That causes a component to be resolved in its own little lifetime. 这导致组件在其自身的短生命期内得以解析。

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 your IOwned interface in your Autofac agnostic assembly. 在与Autofac无关的程序集中定义IOwned接口。
  • 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.

相关问题 每个激活的实例的Autofac Begin Lifetimescope - Autofac begin lifetimescope per activated instance 使用 Autofac 在依赖链中选择 LifetimeScope 的更简洁方法 - Cleaner way to choose LifetimeScope in a dependency chain with Autofac 创建Autofac LifetimeScope之后,是否可以将注册添加到其中? - Is it possible to add registrations to an Autofac LifetimeScope after it was created? 带有OWIN TestServer和AutoFac的WebApi2-LifetimeScope已废弃 - WebApi2 with OWIN TestServer and AutoFac - LifetimeScope already disposed 如何从 autofac 容器解析依赖项(在嵌套的 LifetimeScope 中注册) - How can a dependency (registered in nested LifetimeScope) be resolved from autofac container 如何在创建所有者生命周期范围时自动激活Autofac组件? - How to auto activate Autofac component on creating of Owner lifetimescope? 是否可以使用装饰器类使用Autofac和MediatR为每个事件处理程序创建LifeTimeScope - Is it possible to create a LifeTimeScope per eventhandler with Autofac and MediatR using a decorator class Autofac:有没有办法从Container中获取现有的LifetimeScope,所以我可以避免传递其引用? - Autofac: Is there any way to get an existing LifetimeScope from the Container so I can avoid passing its reference around? 创建容器后如何在autofac中注册类型 - How to register a type with autofac after container creation 错误:创建委托一定不能在Autofac中返回null吗? - Error: The creation delegate must not return null in Autofac?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM