【发布时间】: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