簡體   English   中英

從“ System.String”到“ Sitecore.ContentSearch.ProviderIndexConfiguration”的無效轉換

[英]Invalid cast from 'System.String' to 'Sitecore.ContentSearch.ProviderIndexConfiguration'

我已經在Sitecore 7.2中實現了計算字段索引。 但是,索引管理器現在壞了,我收到以下消息

Invalid cast from 'System.String' to 'Sitecore.ContentSearch.ProviderIndexConfiguration'.
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.InvalidCastException: Invalid cast from 'System.String' to 'Sitecore.ContentSearch.ProviderIndexConfiguration'.

我為我的計算字段實現了以下類:

namespace Computed.Search
{
    public class OfficeLocationComputedField : IComputedIndexField
    {
        public string FieldName { get; set; }
        public string ReturnType { get; set; }

        public object ComputeFieldValue(IIndexable indexable)
        {
            Assert.ArgumentNotNull(indexable, nameof(indexable));
            try
            {
                var result = new List<string>();

                var indexableItem = indexable as SitecoreIndexableItem;
                if (indexableItem == null)
                    return null;

                var item = (Item) indexableItem;

                // Treelist fields map to the MultilistField type as per ~/App_Config/FieldTypes.config
                MultilistField field = item?.Fields["officelocation"];
                if (field == null)
                    return null;

                var items = field.GetItems();
                if (items == null || items.Length == 0)
                    return result;

                foreach (var locationItem in items)
                {
                    //result.Add(locationItem.Name); // if you want the name of the item
                    result.Add(locationItem.DisplayName); // if you want the display name of the item
                    //result.Add(locationItem["Title"]); // if you want the value of a field on the item
                }

                return result;

            }
            catch (Exception exception)
            {
                Log.Error($"An Error occured in custom computed index. {exception.Message}", exception);
            }
            return null;
        }
    }
}

且配置文件如下:

<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <contentSearch>
      <indexConfigurations>
        <defaultLuceneIndexConfiguration>
          <fields hint="raw:AddComputedIndexField">
            <field fieldName="officelocation" storageType="YES" indexType="TOKENIZED">Computed.Search.OfficeLocationComputedField, Computed</field>
          </fields>
        </defaultLuceneIndexConfiguration>
      </indexConfigurations>
    </contentSearch>
  </sitecore>
</configuration>

我不知道自己在犯什么錯誤,或者還需要改變什么?

在90%的情況下,Sitecore Invalid cast from 'System.String' to ...異常是由配置錯誤引起的。

在您的情況下,Sitecore嘗試將字符串強制轉換為ProviderIndexConfiguration 它是從ProviderIndexConfiguration繼承的LuceneIndexConfiguration

看來Sitecore無法將您的補丁文件內容與默認Lucene索引配置匹配。

確保補丁文件名按字母順序在Sitecore.ContentSearch.Lucene.DefaultIndexConfiguration.config之后。

如果問題仍然存在,請將type屬性添加到defaultLuceneIndexConfiguration標記並將其設置為type="Sitecore.ContentSearch.LuceneProvider.LuceneIndexConfiguration, Sitecore.ContentSearch.LuceneProvider"

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <contentSearch>
      <indexConfigurations>
        <defaultLuceneIndexConfiguration type="Sitecore.ContentSearch.LuceneProvider.LuceneIndexConfiguration, Sitecore.ContentSearch.LuceneProvider">
          <fields hint="raw:AddComputedIndexField">
            <field fieldName="officelocation" storageType="YES" indexType="TOKENIZED">Computed.Search.OfficeLocationComputedField, Computed</field>
          </fields>
        </defaultLuceneIndexConfiguration>
      </indexConfigurations>
    </contentSearch>
  </sitecore>
</configuration>

我創建的較舊的修補程序文件按字母順序位於Sitecore.ContentSearch.Lucene.DefaultIndexConfiguration.conf之前,仍位於Website \\ App_Config \\ Include文件夾中。 我忘了刪除它。 刪除該舊文件可解決此問題。 現在一切正常。

暫無
暫無

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

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