简单注入器 - 退出生命周期范围
Simple Injector - exiting a Lifetime Scope
我对文档中的这个声明很感兴趣。
For every request within an implicitly or explicitly defined scope, a
single instance of the service will be returned and that instance will
be disposed when the scope ends.
当我退出 using 块时实际发生了什么。如果我的服务不是 IDisposable,是否只是对象停止被缓存然后被释放用于 GC?
using(container.BeginLifetimeScope())
{
myServiceThatDoesNotImplementIDisposable;
}
谢谢
What actually happens when I exit the using block
当在作用域上调用 Dispose 时(离开 using 块时会发生什么)缓存在该作用域中的所有一次性组件也会被释放,所有创建的作用域实例(包括一次性和非一次性)的缓存清除。这使得这些对象符合垃圾收集的条件。
同样适用于单例,尽管它们的范围围绕容器的整个生命周期。它们在容器被处置时被处置,并且在它们的容器实例被处置时被垃圾收集。
另一方面,容器永远不会跟踪瞬态实例。这意味着它们不存储在缓存中。创建后,容器会立即忘记它们。
我对文档中的这个声明很感兴趣。
For every request within an implicitly or explicitly defined scope, a single instance of the service will be returned and that instance will be disposed when the scope ends.
当我退出 using 块时实际发生了什么。如果我的服务不是 IDisposable,是否只是对象停止被缓存然后被释放用于 GC?
using(container.BeginLifetimeScope())
{
myServiceThatDoesNotImplementIDisposable;
}
谢谢
What actually happens when I exit the using block
当在作用域上调用 Dispose 时(离开 using 块时会发生什么)缓存在该作用域中的所有一次性组件也会被释放,所有创建的作用域实例(包括一次性和非一次性)的缓存清除。这使得这些对象符合垃圾收集的条件。
同样适用于单例,尽管它们的范围围绕容器的整个生命周期。它们在容器被处置时被处置,并且在它们的容器实例被处置时被垃圾收集。
另一方面,容器永远不会跟踪瞬态实例。这意味着它们不存储在缓存中。创建后,容器会立即忘记它们。