【问题标题】:MVCDonutCaching failing when using custom Redis output cache provider使用自定义 Redis 输出缓存提供程序时,MVCDonutCaching 失败
【发布时间】:2019-10-16 04:49:12
【问题描述】:

我有以下自定义 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 = "*")]

失败并出现以下错误:

无法实例化和初始化“xxx.xxx.Web.Caching.CustomRedisOutputCache”类型的 OutputCacheProvider。确保您指定了完整的类型名称。

根据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

【问题讨论】:

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


    【解决方案1】:

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

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

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

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

    【讨论】:

      【解决方案2】:

      好的,getType 返回 null 的原因是因为 nuget 包是在 .net4 中完成的,而使用它的项目是 .net4.6.1,因此 nuget 包无法获取类型,因为类不兼容.由于我拥有源代码,因此我能够在源解决方案中创建自定义 redis 提供程序,并将我的项目和 Web 配置指向更新后的输出

      【讨论】:

        猜你喜欢
        • 2012-12-08
        • 1970-01-01
        • 2017-09-28
        • 2019-08-03
        • 2015-03-22
        • 2014-07-16
        • 2014-02-03
        • 2012-08-02
        • 1970-01-01
        相关资源
        最近更新 更多