簡體   English   中英

使用自定義 Redis 輸出緩存提供程序時 MVCDonutCaching 失敗

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

我有以下自定義 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 包實現 DonutCaching,當我將屬性更改為

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

它因以下錯誤而失敗:

無法實例化和初始化類型為“xxx.xxx.Web.Caching.CustomRedisOutputCache”的 OutputCacheProvider。 確保您指定了完整的類型名稱。

根據文檔,我應該能夠使用自定義緩存提供程序,所以有人知道問題是什么嗎?

查看源代碼,這里似乎失敗了:

            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
                );
            }

上述課程的完整來源

更新

下載並逐步完成源代碼后,即使providerSettings.Type是 xxx.xxx.Web.Caching.CustomRedisOutputCache 並且該類存在於正在執行的項目 xxx.xxx 中, Type.GetType(providerSettings.Type)似乎也返回 null .Web

好的, getType返回 null 的原因是因為 nuget 包是在 .net4 中完成的,而使用它的項目是 .net4.6.1,因此 nuget 包無法獲取類型,因為該類不兼容。 由於我擁有源代碼,因此我能夠在源解決方案中創建一個自定義 redis 提供程序,並將我的項目和 Web 配置指向更新的輸出

我有同樣的問題,使用相同的 MVC Donut Caching 和 RedisOutputCacheProvider 包。 我在MVC 甜甜圈緩存問題列表中找到了解決方案。 似乎我們只需要更具體地了解所引用的類型。

當我將配置類型引用更改為:

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

希望這有助於並消除在您的項目中使用自定義構建或其他人的源代碼的需要。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM