c# - 从 'System.String' 到 'Sitecore.ContentSearch.ProviderIndexConfiguration' 的强制转换无效

标签 c# .net sitecore sitecore7.2

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

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

最佳答案

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

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

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>

关于c# - 从 'System.String' 到 'Sitecore.ContentSearch.ProviderIndexConfiguration' 的强制转换无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44167257/

相关文章:

c# - Unity3d 如何在代码中更改 UI.Button 文本

java - .net 3.5 :how to implement calculate crc32 for data using dot net 3. 5 API?

c# - 查找总和为一个数字的所有组合

c# - .Net Core 在不同操作系统上查找可用磁盘空间

.net - Mango Update 中新的 WP7 API 列表?

c# - 防止 visual studio 将 setter 方法限制为内部

sitecore - 检查 Sitecore 中的链接类型

c# - 将 X-Robot-Tag 添加到 Sitecore 8 中的响应 header

html - 页面编辑器中的 Sitecore 富文本字段在包含 HTML 格式时不可编辑

c# - 如何解决 Funq IoC 中的循环依赖?