【问题标题】:Invalid cast from 'System.String' to 'Sitecore.ContentSearch.ProviderIndexConfiguration'从“System.String”到“Sitecore.ContentSearch.ProviderIndexConfiguration”的无效转换
【发布时间】:2017-05-26 00:12:55
【问题描述】:

我在 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>

我不知道我犯了什么错误或我还需要改变什么?

【问题讨论】:

    标签: c# .net sitecore sitecore7.2


    【解决方案1】:

    Sitecore Invalid cast from 'System.String' to ... 异常在 90% 的情况下是由配置错误引起的。

    在您的情况下,Sitecore 尝试将字符串转换为 ProviderIndexConfiguration。它是LuceneIndexConfiguration,它继承自ProviderIndexConfiguration

    看起来 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/admin/showconfig.aspx的url看看sitecore/contentSearch/indexConfigurations标签的所有子节点是什么?
    • 您也可以尝试将您的字段添加到 Sitecore.ContentSearch.Lucene.DefaultIndexConfiguration.config 而不是自定义补丁文件中,以确保这是配置问题,而不是代码问题。
    【解决方案2】:

    我创建的旧补丁文件按字母顺序排列在 Sitecore.ContentSearch.Lucene.DefaultIndexConfiguration.conf‌​ig 之前仍然在 Website\App_Config\Include 文件夹中。我忘了删除它。删除那个旧文件解决了这个问题。现在一切正常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-26
      相关资源
      最近更新 更多