c# - 使用自定义 Redis 输出缓存提供程序时 MVCDonutCaching 失败

标签 c# redis asp.net-mvc-5 outputcache donut-caching

我有以下自定义 Redis 输出缓存:

public class CustomRedisOutputCache : RedisOutputCacheProvider
{
    public override object Add(string key, object entry, DateTime utcExpiry)
    {
        if (!HttpContextHelper.IsPreview())
        {
            return base.Add(key, entry, utcExpiry);
        }

        return entry;
    }

    public override void Set(string key, object entry, DateTime utcExpiry)
    {
        if (!HttpContextHelper.IsPreview())
        {
            base.Set(key, entry, utcExpiry);
        }
    }
}

在web.config中设置:

<caching>
  <outputCache enableOutputCache="false" defaultProvider="CustomRedisOutputCache">
    <providers>
      <add name="CustomRedisOutputCache" type="xxx.xxx.Web.Caching.CustomRedisOutputCache" applicationName="xxx.xxx" connectionString="Redis" databaseId="4" throwOnError="false" retryTimeoutInMilliseconds="5000" loggingClassName="" loggingMethodName="" />
    </providers>
  </outputCache>

当我使用 outputcache 属性时一切正常:

[OutputCache(CacheProfile = CacheProfile.Medium, VaryByParam = "*")]

但是,我正在尝试使用 MVCDonutCaching Nuget Package 实现 DonutCaching当我将属性更改为

[DonutOutputCache(CacheProfile = CacheProfile.Medium, VaryByParam = "*")]

失败并出现以下错误:

Unable to instantiate and initialize OutputCacheProvider of type 'xxx.xxx.Web.Caching.CustomRedisOutputCache'. Make sure you are specifying the full type name.

根据documentation我应该能够使用自定义缓存提供程序,所以有人知道问题出在哪里吗?

查看源码,好像是这里失败了:

            try
            {
                Instance = (OutputCacheProvider)Activator.CreateInstance(Type.GetType(providerSettings.Type));
                Instance.Initialize(providerSettings.Name, providerSettings.Parameters);

            }
            catch (Exception ex)
            {
                throw new ConfigurationErrorsException(
                    string.Format("Unable to instantiate and initialize OutputCacheProvider of type '{0}'. Make sure you are specifying the full type name.", providerSettings.Type),
                    ex
                );
            }

Full source for the above class

更新

下载并单步执行源代码后,似乎 Type.GetType(providerSettings.Type) 返回 null,即使 providerSettings.Type 是 xxx.xxx .Web.Caching.CustomRedisOutputCache 和该类存在于正在执行的项目 xxx.xxx.Web

最佳答案

我有同样的问题,使用相同的 MVC Donut Caching 和 RedisOutputCacheProvider 包。我在 MVC Donut Caching issue list 上找到了解决方案.看来我们只需要更具体地说明所引用的类型即可。

当我将我的配置类型引用更改为:

type="Microsoft.Web.Redis.RedisOutputCacheProvider, Microsoft.Web.RedisOutputCacheProvider"

希望这有助于消除在您的项目中使用自定义构建或其他人源代码的需要。

关于c# - 使用自定义 Redis 输出缓存提供程序时 MVCDonutCaching 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56379793/

相关文章:

c# - 如何创建一个接受 Controller 参数而不更改子 Controller 的基类?

c# - 如何在 ASP.NET MVC 5、Entity Framework 6 中使用流畅的 API 映射表?

css - 如何在 MVC5 中切换 _Layout View

c# - 如何延迟 Unity 和 C# 中的方法?

c# - RenderTargetBitmap 内存泄漏

c# - 如何确保我的 LINQ 查询在我的 DAL 中调用时执行,而不是以延迟方式执行?

Node.js:通过 cluster.fork() 调用不同的行为

redis - 将 Redis 查询输出保存到文件

c# - 您能否将 'It' 匹配器对象存储为变量,而不是将其内联定义? (起订量)

redis - 为什么单个 Redis 实例不是线程安全的?