November Happy Hour will be moved to Thursday December 5th.

ProjectionException - Issue with deserialization

Vote:
 

I have a type WebProduct where I have added a property that I want to add to the indexed object. The property looks like this:

private IList<StoreStockThin> _inStockInStores = null;
        [Ignore]
        public IList<StoreStockThin> InStockInStores
        {
            get
            {
                if (_inStockInStores == null)
                {
                    _inStockInStores = _productService.Service.GetInStockForStores(this).ToList();
                }

                return _inStockInStores;
            }
        }

I have also addded the following constructor 

[JsonConstructor]
        public WebProduct(IList<StoreStockThin> inStockInStores)
        {
            _inStockInStores = inStockInStores;
        }

The data is indexed properly 

"InStockInStores$$nested": [
        {
            "StoreName$$string": "Store B",
            "___types": [
                "X.Features.Product.Models.StoreStockThin",
                "System.Object"
            ],
            "StoreOrgUnitNumber$$string": "9932",
            "$type": "X.Features.Product.Models.StoreStockThin, A.Project"
        },
        {
            "StoreName$$string": "Store B",
            "___types": [
                "X.Features.Product.Models.StoreStockThin",
                "System.Object"
            ],
            "StoreOrgUnitNumber$$string": "9932",
            "$type": "X.Features.Product.Models.StoreStockThin, A.Project"
        }
]

When listing products and data comes back from Find, the following exception is thown:

EPiServer.Find.ProjectionException: An exception of type InvalidCastException was thrown while deserializing object. ---> System.InvalidCastException: Unable to cast object of type 'WhereSelectEnumerableIterator`2[System.Globalization.CultureInfo,System.String]' to type 'System.Collections.IList'.
   ved Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObjectUsingCreatorWithParameters(JsonReader reader, JsonObjectContract contract, JsonProperty containerProperty, ObjectConstructor`1 creator, String id)
   ved Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
   ved Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
   ved Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
   ved EPiServer.Find.ProjectionHelper.DeserializeObject(JToken root)
   --- Slutten på sporingen av intern unntaksstakk ---
   ved EPiServer.Find.ProjectionHelper.DeserializeObject(JToken root)
   ved EPiServer.Find.ProjectionHelper.AddPartialFields(SearchRequestBody requestBody, SearchHit`1 searchHit, Dictionary`2 args)
   ved EPiServer.Find.ProjectionHelper.GetFieldValues(SearchRequestBody requestBody, SearchHit`1 searchHit)
   ved EPiServer.Find.ProjectionHelper.GetMappedSearchResultItem[TResult](SearchRequestBody requestBody, SearchHit`1 searchHit)
   ved EPiServer.Find.ProjectionHelper.GetMappedResult[TResult](SearchRequestBody requestBody, SearchResults`1 jsonResult)
   ved EPiServer.Find.MultiSearchExtensions.GetResult[TResult](IMultiSearch`1 multiSearch) 

I have tried changing property from IList to List, but that does not affect outcome.

Anyone have any inputs to why this is happening?

#254675
May 11, 2021 9:10
Vote:
 

You might want to change your property to IEnumerable<T> instead (while keep the internal type to be IList<>) 

#254676
May 11, 2021 9:41
Vote:
 

I tried that as well, same error.

#254677
May 11, 2021 10:49
Vote:
 

Just to be sure: are you sure the problem is with that property? Could you try removing it to see what happens?

#254679
May 11, 2021 11:39
Vote:
 

Yes, the error message confused me a bit, so I did that to be sure. If I remove it, page loads without errors. 
If I try to remove JsonConstructor it cannot deserialize the property and instead goes ahead and tries to call productService to populate it.

#254680
May 11, 2021 12:09
Vote:
 

We solved this rewriting how we added fields to the indexed model - I wrote a blog posts about the topic, read it here: https://www.getadigital.com/blog/enrich-models-upon-indexing/

#256170
Jun 07, 2021 13:08
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.