我正在将一项服务作为 Scoped 添加到启动中的 IServiceCollection:
public void ConfigureServices(IServiceCollection services)
{
services.AddScoped<IEmailService, EmailService>();
}
如果我要向我的 EmailService 实现添加一个静态属性,这是否意味着我的属性不会被垃圾收集并保留在内存中,即使我的 EmailService 在我的请求结束后超出了范围?
我正在查看一些具有保存不记名 token 的静态属性的代码。看来这个 token 一旦设置就会保留在内存中,并保留在那里,直到一天后我的应用程序池刷新。
最佳答案
does that mean that my property is not garbage collected and remains in memory even though my EmailService has gone out of scope after my request ends?
是的
参见here :
"static members are associated with the Type, which is associated with the AppDomain it's loaded in .. Note that there doesn't [even] have to be any instances .. for the class to be initialized"
关于c# - 在 Scoped 服务中使用静态属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53786106/