简体   繁体   English

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

[英]MVCDonutCaching failing when using custom Redis output cache provider

I have the following custom Redis output cache:我有以下自定义 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);
        }
    }
}

Which is set up in the web.config:这是在 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>

Which all works fine when I use the outputcache attribute:当我使用 outputcache 属性时,一切正常:

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

However, I'm trying to implement DonutCaching with the MVCDonutCaching Nuget Package and when I change the attribute to但是,我正在尝试使用MVCDonutCaching Nuget 包实现 DonutCaching,当我将属性更改为

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

It is failing with the following error:它因以下错误而失败:

Unable to instantiate and initialize OutputCacheProvider of type 'xxx.xxx.Web.Caching.CustomRedisOutputCache'.无法实例化和初始化类型为“xxx.xxx.Web.Caching.CustomRedisOutputCache”的 OutputCacheProvider。 Make sure you are specifying the full type name.确保您指定了完整的类型名称。

According to the documentation I should be able to use a custom cache provider, so does anyone know what the problem is?根据文档,我应该能够使用自定义缓存提供程序,所以有人知道问题是什么吗?

Looking at the source code, it seems to be failing here:查看源代码,这里似乎失败了:

            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 上述课程的完整来源

Update更新

After downloading and stepping through the source code, it would seem the Type.GetType(providerSettings.Type) is returning null even though providerSettings.Type is xxx.xxx.Web.Caching.CustomRedisOutputCache and that class exists in the executing project xxx.xxx.Web下载并逐步完成源代码后,即使providerSettings.Type是 xxx.xxx.Web.Caching.CustomRedisOutputCache 并且该类存在于正在执行的项目 xxx.xxx 中, Type.GetType(providerSettings.Type)似乎也返回 null .Web

Ok the reason why the getType was returning null is because the nuget package is done in .net4 and the project using it was .net4.6.1 and therefore the nuget package was unable to get the type because the class was incompatible.好的, getType返回 null 的原因是因为 nuget 包是在 .net4 中完成的,而使用它的项目是 .net4.6.1,因此 nuget 包无法获取类型,因为该类不兼容。 As I had the source, I was able to create a custom redis provider in the source solution and just point my project and web config at the updated output由于我拥有源代码,因此我能够在源解决方案中创建一个自定义 redis 提供程序,并将我的项目和 Web 配置指向更新的输出

I had the same issue, using the same MVC Donut Caching and RedisOutputCacheProvider packages.我有同样的问题,使用相同的 MVC Donut Caching 和 RedisOutputCacheProvider 包。 I found the solution on the MVC Donut Caching issue list .我在MVC 甜甜圈缓存问题列表中找到了解决方案。 It seems that we just need to be more specific about the type being referenced.似乎我们只需要更具体地了解所引用的类型。

It worked for me when I changed my config type reference to:当我将配置类型引用更改为:

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

Hopefully this helps and removes the need to have a custom build or someone else's source in your project.希望这有助于并消除在您的项目中使用自定义构建或其他人的源代码的需要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM