- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
统一 2.0:
默认情况下 RegisterInstance 使用 ContainerControlledLifetimeManager。当 Unity 容器被释放时,它会在实例上调用 Dispose(如果 IDisposable)。
在我的情况下,这不是我想要的。该实例由另一个类拥有和处置; Unity 应该只注入(inject)引用。所以我用了:
container.RegisterInstance(instance, new ExternallyControlledLifetimeManager());
Unity 文档(在 Understanding Lifetime Managers 下)指出:
Using the RegisterInstance method to register an existing object results in the same behavior as if you just registered the lifetime container with RegisterType. Therefore, it is recommended that you do not use the RegisterInstance method to register an existing object when using the non-default lifetime managers except for the thread in which the RegisterInstance was invoked.
这是什么意思?
同一节还指出:
If you registered an existing instance of an object using the RegisterInstance method, the container returns the same instance for all calls to Resolve or ResolveAll or when the dependency mechanism injects instances into other classes, provided that one of the following is true:
- You have specified a container-controlled lifetime manager
- You have used the default lifetime manager
- You are resolving in the same context in which you registered the instance when using a different lifetime manager.
在将 RegisterInstance 与 ExternallyControlledLifetimeManager 一起使用后,我尝试在不同的线程中解析,它成功了 - 我得到了单例实例。
我的代码与创建实例注册部分中的示例匹配。不过,我想确保我理解上下文警告。
需要明确的是,我始终希望 Unity 容器注入(inject)我注册的实例,而不考虑线程等,而且我不希望 Unity 处理它。我这样做正确吗?
最佳答案
注意 ExternallyControlledLifetimeManager
。您仍然必须在容器外部某处保存对实例的引用。一旦失去引用,您就会失去实例,因为 ExternallyControlledLifetimeManager
只持有 WeakReference
。如果您没有正常的引用,垃圾收集器可以收集您的实例。检查example在我的博客上。
关于c# - unity RegisterInstance IDisposable对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5330639/
据我所知,这是一个公认的规则,如果你有一个具有 IDisposable 成员 m 的类 A,A 应该实现 IDisposable 并且它应该在其中调用 m.Dispose()。 我找不到令人满意的理由
我有一个父类和子类都需要实现IDisposable。 virtual(和 base.Dispose()?)调用应该在哪里发挥作用?当我只是重写 Dispose(bool disposing) 调用时,
我意识到这个问题正在寻找的查询不足以找到 IDisposable 实现的每一个小问题,但每个早期警告都很重要,所以我会尽我所能。 我想知道是否有人提出了 NDepend 的 CQL 查询,该查询将列出
我对 C# 还是比较陌生,只是在过去几天才接触到“IDisposables”。我可以掌握 using block 的概念来处理必须处理的对象,而无需手动记住调用 .Dispose() 方法 - 方便!
在我的应用程序中,我有一个每隔几秒创建一次的大对象。我用它做了一些工作,然后就不再需要它了。 我在任务管理器中看到,即使我没有对该对象的任何引用并且需要收集它,ram 大小也会增加。 在实现 IDis
考虑一下: interface IFoo : IDisposable { } class Program { static void Main() { var foo = GetFoo
我是这么认为的。但是请看一下 ASP.NET 中的内置类: public sealed class HttpPostedFile { public Stream InputStream { g
昨天,在我们的代码库上运行 Visual Studio 代码分析后,以下代码被突出显示为一个问题: using (var stringReader = new StringReader(someStr
我认为这个问题说明了一切。 谢谢。 最佳答案 如果对象拥有该资源,他们应该实现IDisposable。通常不需要将事物设置为 null,尽管它没有坏处。 如果您不拥有该资源,那么显然您不应该处置它,并
MSDN 文档和 StackOverflow 上的许多答案竭尽全力讨论如何正确实现 IDisposable,例如MSDN IDisposable , MSDN Implementing IDispos
如果一个类继承自一个本身实现了 IDisposable 的接口(interface),该类是否也应该实现 IDisposable? 例如 internal IMyInterface : IDispos
当 出现时,正确处理接口(interface)的默认实现的最佳方法是什么?接口(interface)不继承自 IDisposable ?例如,假设我想做 public class FooGetter
我有一个实现 IDisposable 的类,因为它有一个 IDisposable 的私有(private)成员字段“foo”(在构造函数中初始化)。我意外地收到了 CA2000 代码分析错误,它希望我
如果我有一个相当标准的抽象类,它允许注入(inject)一个 IDisposable 实例。但是,一些继承自此类的类不应处置注入(inject)的存储库,而其他类则应处置。显而易见的解决方案是有一个构
这个问题在这里已经有了答案: Declare IDisposable for the class or interface? (7 个答案) 关闭 4 年前。 我想问的是,如果类 A 继承自 Int
我有一个“永远”存在于应用程序中的 IDiposable 对象。在我的例子中是 SemaphoreSlim,但这个问题实际上适用于任何可以处置的对象。 我确信只要应用程序运行,我就需要一个对象。所以问
我有一堆 IDisposable查找表中的对象(现在是普通的旧字典<>),但为了简化代码并避免错误,我正在寻找一个“拥有”它所拥有的项目的集合类,并避免重新发明轮子 - 这样做一个类已经存在? 规范应
很久以前来自 C/C++ 的我仍然有确保所有资源都被正确清理的习惯。我总是确保在 IDisposable 类上调用 Dispose 并在包含一次性对象的类中实现 Dispose 模式。 但是,在我的环
如果我有实现 IDisposable 的接口(interface)(来自 apress 书中的示例)像这样 public interface IArchitectRepository : IDispo
问题是如何测试调用 Finalize 时对象释放资源的事实。类的代码: public class TestClass : IDisposable { public bool HasBeenDi
我是一名优秀的程序员,十分优秀!