c# - 在 Scoped 服务中使用静态属性

标签 c# asp.net-core dependency-injection static

我正在将一项服务作为 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/

相关文章:

c# - 如何将依赖项注入(inject)实现接口(interface)的类?

java - Spring - 以编程方式将接口(interface)绑定(bind)到实现类

language-agnostic - 服务定位器不仅仅是全局变量/状态吗?

c# - .NET Core 2.0 分离 Startup.cs 服务注入(inject)

json - 如何在 ASP.NET Core WebAPI 中使用 Newtonsoft : Json. NET?

c# - DirectShow 在我用来解密缓冲流的源过滤器 (UNSUPPORTED_STREAM) 上抛出错误

c# - 允许空外键 GUID

asp.net - .NET Core 2 CookieAuthentication 忽略过期时间跨度

c# - 如何获取 WSHttpBinding 安全属性?

c# - 使用 LINQ Where/Take 返回最相关的搜索结果