gpt4 book ai didi

c# - AddSingleton - 生命周期 - 类是否需要实现 IDisposable?

转载 作者:行者123 更新时间:2023-12-05 02:11:32 24 4
gpt4 key购买 nike

当为使用 AddScopedAddSingleton 添加的服务使用 DI 时,该服务是否需要实现 IDisposable(即使它没有使用任何非托管资源(如文件)?

从 Microsoft Docs 中找到以下示例:

// Services that implement IDisposable:
public class Service1 : IDisposable {}
public class Service2 : IDisposable {}
public class Service3 : IDisposable {}

public interface ISomeService {}
public class SomeServiceImplementation : ISomeService, IDisposable {}

public void ConfigureServices(IServiceCollection services)
{
// The container creates the following instances and disposes them automatically:
services.AddScoped<Service1>();
services.AddSingleton<Service2>();
services.AddSingleton<ISomeService>(sp => new SomeServiceImplementation());

// The container doesn't create the following instances, so it doesn't dispose of
// the instances automatically:
services.AddSingleton<Service3>(new Service3());
services.AddSingleton(new Service3());
}

如果我有这段代码会发生什么:

public class Service0  // (doesn't implement Disposable) 

services.AddScoped<Service0>(); // what happens to it when request scope ends? Does it stay on the heap?

services.AddSingleton<Service0>(); // it lives till application dies
services.AddSingleton(new Service0()); // ??
services.AddSingleton<IConfigureOptions<Service0>>((ctx) =>
{
return new ConfigureNamedOptions<Service0>(null, (config) =>
{
// Do something here -- in debug mode it is executing this logic for each request
}} // it is returning "new Service0" when a request is made. Does it mean for each request it returns new object and keeps in heap memory or returns same previously created object?

最佳答案

does the service need to implement IDisposable(even if it is not using any unmanaged resources like files)

通常不会,因为 IDisposable 的主要目的是允许释放非托管资源。然而,实现 IDisposable 还有一个额外的原因。 . Dispose()方法有时用作完成在构造函数中启动的操作的 Hook 。例如,构造函数开始持续时间测量,而 Dispose()停止测量并将持续时间报告给某些监控机制。

一些关于 IDisposable 的背景知识

如果对象没有实现 IDisposable这并不意味着它留在堆中。事实上,GC 甚至不知道是什么 IDisposable是。这个接口(interface)只是一个模式。但是,编译器知道 IDisposable , 它发出对 Dispose() 的调用在using结束语句范围。

此外,在许多情况下,基础设施层或库(例如 ASP.NET Core 中的 DI)会检查对象是否实现了 IDisposable ,如果是,请调用 Dispose()在上面。

所以一个对象实现了IDisposable本身并不能保证 Dispose()将在 GC 之前调用。这取决于对象的用户。实际确保Dispose()在 GC 之前,一次性模式的完整实现包括调用 Dispose()来自“析构函数”。

what happens to it when request scope ends? stays in heap? what happens to it when request scope ends?

  • AddScoped<Service0>() :在请求结束时,对对象的引用被“遗忘”(GC 可以随时删除它)。就在忘记引用之前,检查对象是否实现了 IDisposable。 ,如果是,Dispose()将被调用。

  • AddSingleton<Service0>() :在网络主机生命周期结束时,对对象的引用被“遗忘”(GC 可以随时删除它)。就在忘记引用之前,检查对象是否实现了 IDisposable。 ,如果是,Dispose()将被调用。

  • AddSingleton(new Service0()) :在网络主机生命周期结束时,对对象的引用被“遗忘”(GC 可以随时删除它)。但是由于这个对象是从外部提供的,没有被 DI 实例化,所以不会检查 IDisposable。和 Dispose不会被调用。

关于c# - AddSingleton - 生命周期 - 类是否需要实现 IDisposable?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57447881/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com