我有以下自定义 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/