c# - 在 ASP.NET 5/MVC 6 中使用 IDistributedCache 和 Redis - 类型或命名空间 'Microsoft.Caching' 不存在

标签 c# caching asp.net-core asp.net-core-mvc

我正在尝试在 MVC 6 中使用 Redis 缓存,但我无法构建我的项目。

我遵循了 https://github.com/aspnet/Caching/tree/dev/samples 中的示例- 有一个 IDistributedCache 示例和一个 Redis 示例。但是,这并不完全清楚,因为 Redis 示例不是 Web 应用程序。

我的 project.json 依赖项中有这个:

    "Microsoft.Framework.Cache.Memory": "1.0.0-beta3",
    "Microsoft.Framework.Caching.Distributed": "1.0.0-*",
    "Microsoft.Framework.Caching.Redis": "1.0.0-*",

我用它来获取我的包裹:

kpm install Microsoft.Framework.Caching.Distributed

kpm install Microsoft.Framework.Caching.Redis

两个命令都有效并返回了 OK。

在我的初创公司中,我正在尝试以下操作:

 services.AddSingleton<IMemoryCache, MemoryCache>();
 services.AddSingleton<IDistributedCache, RedisCache>();

尝试构建或运行时,给出的错误是:

Startup.cs(10,27): error CS0234: The type or namespace name 'Caching' does not exist in the namespace 'Microsoft.Framework' (are you missing an assembly reference?) Startup.cs(11,27): error CS0234: The type or namespace name 'Caching' does not exist in the namespace 'Microsoft.Framework' (are you missing an assembly reference?) Startup.cs(25,35): error CS0246: The type or namespace name 'IDistributedCache' could not be found (are you missing a using directive or an assembly reference?) Startup.cs(25,54): error CS0246: The type or namespace name 'RedisCache' could not be found (are you missing a using directive or an assembly reference?)

现在显然我有错误的引用 - 但正确的引用是什么?我是直接从示例中复制 namespace 吗?我尝试切换到 Microsoft.Cache.Distributed 但这也不起作用(并且与 github 冲突)

我觉得这与没有正确的 K 版本有关,也许这就是出路 - 升级到最新版本。 Framework.Cache.Memory 和 Framework.Cache.Distributed 之间存在不匹配,我看到了与 namespace 更改相关的 github 评论。

如果有人能指出走出这个迷宫的路,非常感谢。

最佳答案

The below answer applies for 1.0.0-beta4 release of caching library which corresponds to here: https://github.com/aspnet/Caching/tree/1.0.0-beta4

IMemoryCacheMicrosoft.Framework.Caching.Memory 命名空间和 IDistributedCache 下在 Microsoft.Framework.Caching.Distributed 命名空间下。

依赖关系看起来像这样:

"dependencies": { 
    "Microsoft.Framework.Caching.Memory": "1.0.0-beta4",
    "Microsoft.Framework.Caching.Distributed": "1.0.0-beta4",
    "Microsoft.Framework.Caching.Redis": "1.0.0-beta4"
}

因此,Microsoft.Framework.Cache.MemoryMicrosoft.Framework.Caching.Memory

关于c# - 在 ASP.NET 5/MVC 6 中使用 IDistributedCache 和 Redis - 类型或命名空间 'Microsoft.Caching' 不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30325970/

相关文章:

c# - C#中的模式匹配问题

c# - parse() 处的 JsonReaderException - HololensDeploy

php - 在 yii2 中使用缓存时呈现动态 csrf 隐藏输入

c++ - 判断缓存是回写还是通过

c# - WCF操作合约: Optional Parameter Support?

c# - 如何在 C# 中创建一个变量来保存日期?

ruby-on-rails - 在 Rails 中,我可以在 View 中检查是否要缓存操作吗?

c# - 升级到 v1.1 后,ASP.NET Core 无法在 IIS 中运行

asp.net-core - 如何在ASP.NET Core中使用JWT授权重定向到401上的登录页面

asp.net - 在线用户数