[英]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.