Thursday, September 19, 2013

"This operation is only valid on generic types." Exception in Sitecore 7 (Initial release)

Have you ever run into "This operation is only valid on generic types." exception? My team has. I believe its a bug with Sitecore code where IndexFieldEnumerableConverter calls GetGenericTypeDefinition method without checking if the type of the property is in fact generic.

The reason why we ran into this issues was the type of collection properties on classes that search results from Solr are being mapped to. Initially List type was used. Updating all properties from List to IEnumerable solved the issue.

Server Error in '/' Application.
This operation is only valid on generic types.
Description: An unhandled exception occurred.
Exception Details: System.InvalidOperationException: This operation is only valid on generic types.
...
Stack Trace:
[InvalidOperationException: This operation is only valid on generic types.]
System.RuntimeType.GetGenericTypeDefinition() +13998682
Sitecore.ContentSearch.Converters.IndexFieldEnumerableConverter.GetElementType(Type destinationType) +389
Sitecore.ContentSearch.Converters.IndexFieldEnumerableConverter.GetListAndElementType(Type destinationType, Type& elementType, Type& listType) +156
Sitecore.ContentSearch.Converters.IndexFieldEnumerableConverter.ConvertTo(ITypeDescriptorContext context, CultureInfo culture, Object value, Type destinationType) +288
Sitecore.ContentSearch.Converters.IndexFieldStorageValueFormatter.ReadFromIndexStorage(Object indexValue, Type destinationType) +355
Sitecore.ContentSearch.DocumentTypeMapInfo.SetProperty(Object target, String propertyName, String documentFieldName, Object value) +544
Sitecore.ContentSearch.SolrProvider.Mapping.SolrDocumentPropertyMapper.ReadDocumentFields(Dictionary`2 document, IEnumerable`1 fieldNames, DocumentTypeMapInfo documentTypeMapInfo, IEnumerable`1 virtualFieldProcessors, TElement result) +942
Sitecore.ContentSearch.DefaultDocumentMapper`1.MapToType(TDocument document, SelectMethod selectMethod, IEnumerable`1 virtualFieldProcessors, SearchSecurityOptions securityOptions) +283
Sitecore.ContentSearch.SolrProvider.<GetSearchResults>d__a.MoveNext() +977
System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) +536
System.Linq.Enumerable.ToList(IEnumerable`1 source) +80

...