简体   繁体   English

如果*不*注入依赖项,是否可以在 ASP.NET Core 中使用 IMemoryCache?

[英]Possible to use IMemoryCache in ASP.NET Core if it is *not* dependency injected?

So I have a need to use IMemoryCache in my ASP.NET Core application, in most places it is DI'd into the controllers and works as expected.所以我需要在我的 ASP.NET Core 应用程序中使用 IMemoryCache,在大多数地方它被 DI 到控制器中并按预期工作。 Now I have a need to cache part of the menu, and the menu logic is called as part of the layout view.现在我需要缓存部分菜单,菜单逻辑作为布局视图的一部分被调用。 So, there isn't a controller to speak of.所以,没有控制器可言。 My Startup has the following in it already, which works well for other things:我的 Startup 已经包含以下内容,它适用于其他事情:

services.AddMemoryCache();

The call that is part of the layout looks like this:作为布局一部分的调用如下所示:

@(await new MenuUtility().DisplayNavBar(Html, showMenus))

The MenuUtility class, which is where the cache will be used, has a call that looks like this:将使用缓存的 MenuUtility 类具有如下所示的调用:

public class MenuUtility
{
  private IMemoryCache _cache;

    public MenuUtility()
    {
       _cache = HttpContext.Current.RequestServices.GetService<MemoryCache>();
    }

    // Rest of class logic
}

The problem is, the cache service I am looking to return is always null.问题是,我希望返回的缓存服务始终为空。 Is there something I am forgetting?有什么我忘记了吗? I tried to add services.AddScoped<MenuUtility>();我试图添加services.AddScoped<MenuUtility>(); to the ConfigureServices call, thinking that this would allow me to pass the cache interface in the constructor, but since I am calling the MenuUtility explicitly I don't have the parameter of the cache to pass in.到 ConfigureServices 调用,认为这将允许我在构造函数中传递缓存接口,但是由于我显式调用 MenuUtility,因此我没有要传入的缓存参数。

I see a few questions here in SO that talk around this issue, but nothing that directly and correctly answers it.我在这里看到一些关于这个问题的问题,但没有直接和正确地回答它。

Since this is my project for which I will forever be the only programmer, I would prefer a simple solution instead of suggestions to rearchitect a bunch of it, if I can't come up with a good solution I will just leave out the cache for this.由于这是我的项目,我将永远是唯一的程序员,我更喜欢一个简单的解决方案,而不是建议来重新构建一堆它,如果我不能想出一个好的解决方案,我将不考虑缓存这个。

the menu logic is called as part of the layout view菜单逻辑作为布局视图的一部分被调用

You can still use the @inject directive inside layout Razor views to inject services into the view:您仍然可以在布局 Razor 视图中使用@inject指令将服务注入视图:

@inject MenuUtility menuUtility

@await menuUtility.DisplayNavBar(Html, showMenus)

To make that work, you will just need to register the MenuUtility as a service and then use constructor injection in your MenuUtility to resolve the IMemoryCache :要使其工作,您只需要将MenuUtility注册为服务,然后在MenuUtility使用构造函数注入来解析IMemoryCache

// in Startup.ConfigureServices
services.AddTransient<MenuUtility>();
public class MenuUtility
{
    private readonly IMemoryCache _cache;

    public MenuUtility(IMemoryCache cache)
    {
       _cache = cache;
    }

    // …
}

You should generally avoid having to create utilities and services using new .您通常应该避免使用new创建实用程序和服务。 The chance is high that you do have some dependency on some framework component, and then you should definitely use dependency injection to resolve this dependency automatically.很有可能你确实对某个框架组件有一些依赖,那么你绝对应该使用依赖注入来自动解决这个依赖。

Btw.顺便提一句。 HttpContext.Current should not compile in ASP.NET Core. HttpContext.Current不应在 ASP.NET Core 中编译 If it does, chances are that you are running on the .NET Framework with System.Web referenced.如果是这样,很可能是您在引用System.Web的 .NET Framework 上运行。 That is the old ASP.NET which is completely incompatible to ASP.NET Core.那是与 ASP.NET Core 完全不兼容的旧 ASP.NET。 You should absolutely not mix these two frameworks.你绝对不应该混合这两个框架。 In ASP.NET Core, there is no way to retrieve the current HttpContext statically.在 ASP.NET Core 中,无法静态检索当前 HttpContext。 You have to use dependency injection at some point.必须在某些时候使用依赖注入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 ASP.net 核心应用程序中使用 IMemoryCache 和 Unity DI Container - Use IMemoryCache with Unity DI Container in ASP.net core application 依赖注入UserManager正在处理异步调用(ASP.NET CORE) - Dependency Injected UserManager is disposing on async call (ASP.NET CORE) XUnit如何模拟IMemoryCache ASP.NET Core - XUnit how to mock IMemoryCache ASP.NET Core ASP.NET 内核将带 IMemoryCache 的内存缓存转换为 Redis - ASP.NET Core Converting In-Memory Cache with IMemoryCache to Redis 为什么在ASP.Net Core中获取IMemoryCache的多个实例? - Why getting multiple instances of IMemoryCache in ASP.Net Core? 测试ASP.NET Core IMemoryCache的正确方法 - Proper way of testing ASP.NET Core IMemoryCache 如何检查是否在 ASP.NET Core 中注入了依赖项注入服务? - How do I check if a dependency injection service was injected in ASP.NET Core? ASP.NET 内核 - 更新缓存列表的 1 个元素<element>在 IMemoryCache 中使用 EF Core</element> - ASP.NET Core - Update 1 Element of a cached List<Element> in IMemoryCache using EF Core 如何从 ASP.NET 核心中的 IMemoryCache 中删除所有对象(重置) - How to remove all objects (reset ) from IMemoryCache in ASP.NET core 如何在IHttpcontextAccessor的ASP.NET Core 2.0中使用依赖项注入 - How to use dependency injection in ASP.NET Core 2.0 for IHttpcontextAccessor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM