gpt4 book ai didi

c# - 在我的 ASP.NET 应用程序中的什么地方注册 IMemoryCache?

转载 作者:太空宇宙 更新时间:2023-11-03 12:42:09 25 4
gpt4 key购买 nike

我有一个从 Visual Studio 2015 Community Edition 创建的 ASP.NET Web 应用程序。 .NET Framework 是 4.6.1。

我不知道我的 Web 应用程序使用的是哪个版本的 ASP.NET MVC 4? ASP.NET MVC 5?任何地方都没有提到它。

我正在尝试将 IMemoryCache 服务(来自 Microsoft.Extensions.Caching.Memory)注册到我的 Microsoft Unity 容器中。但是,我在 Google 上发现的任何内容都涉及在 Startup.cs 文件中添加 services.AddCaching()

我没有 Startup.cs 文件。我只看到 Global.asax。此外,我所有的自定义依赖项都在 UnityConfig.cs 中注册,这是在安装 NuGet 包 Unity.Mvc 时提供的,它是 Microsoft 的 ASP.NET 的 Microsoft Unity Bootstrap 。

感谢任何帮助。

编辑:

这是我的项目的截图:

enter image description here

最佳答案

在 .NET Core 中,AddMemoryCache 扩展如下所示:

services.TryAdd(ServiceDescriptor.Singleton<IMemoryCache, MemoryCache>());

所以在 .Net Framework 中你可以使用这样的东西(对于 Autofac):

builder.RegisterType<MemoryCache>().As<IMemoryCache>().SingleInstance();

UPD:但是MemoryCache取决于 IOptions<>你也应该注册它:

builder.RegisterGeneric(typeof(OptionsManager<>)).As(typeof(IOptions<>)).SingleInstance();
builder.RegisterGeneric(typeof(OptionsManager<>)).As(typeof(IOptionsSnapshot<>)).InstancePerLifetimeScope();
builder.RegisterGeneric(typeof(OptionsMonitor<>)).As(typeof(IOptionsMonitor<>)).SingleInstance();
builder.RegisterGeneric(typeof(OptionsFactory<>)).As(typeof(IOptionsFactory<>));
builder.RegisterGeneric(typeof(OptionsCache<>)).As(typeof(IOptionsMonitorCache<>)).SingleInstance();

关于c# - 在我的 ASP.NET 应用程序中的什么地方注册 IMemoryCache?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38570125/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com