I have the following custom Redis output cache:
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:
<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(CacheProfile = CacheProfile.Medium, VaryByParam = "*")]
However, I'm trying to implement DonutCaching with the MVCDonutCaching Nuget Package and when I change the attribute to
[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'. 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
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. 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