【问题标题】:IMemoryCache gets injected without adding itIMemoryCache 在不添加的情况下被注入
【发布时间】:2018-12-01 19:04:15
【问题描述】:

我已将我的应用程序从 MemoryCache 切换到 DistributedSqlServerCache。在注释掉 services.AddMemoryCache() 之后,我注意到 IMemoryCache 仍然被注入到我的类中。我使用 aspnet-core 2.1。

将缓存添加到服务的代码:

//services.AddMemoryCache();
services.AddDistributedSqlServerCache(o =>
{
    o.ConnectionString = ConnectionString;
    o.SchemaName = "dbo";
    o.TableName = "tbSessionCache";
});

证明它仍然被注入:

为什么会这样?我想我可能不明白这是如何工作的。非常感谢!

【问题讨论】:

  • MVC 使用IMemoryCache,所以即使你没有显式加载它仍然可用。
  • 好的,预期的答案提供了解释,但是您有解决方案吗?如果是这样,请发布自我回答。
  • 我只搜索了一个解释,这是在接受的答案中提供的。如果你想防止这种行为,我认为你可以创建一个派生自 IMemoryCache 的接口(例如 IHttpCustomMemoryCache),并用它围绕 IMemoryCache 构建一个包装服务。不知道这是否漂亮。

标签: c# asp.net-mvc asp.net-core dependency-injection


【解决方案1】:

【讨论】:

  • std IO 容器中的任何诸如删除或清除之类的东西?
猜你喜欢
  • 1970-01-01
  • 2013-10-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-30
  • 1970-01-01
  • 2022-06-16
相关资源
最近更新 更多