3

I'm using Glass 4.2.0.184 with Sitecore 8.1 Update 2. I'm receiving an error in an API controller setup with a helper function that's retrieving items from search and then casting them to a model. This functionality is working in our QA and CD environments, but locally I keep getting the error. It boils down to a failed to map properties on [item path] error, and the consistent error is:

"ExceptionType":"Glass.Mapper.MapperException","StackTrace":"   at Glass.Mapper.Configuration.AbstractTypeConfiguration.MapPropertiesToObject(Object obj, IAbstractService service, AbstractTypeCreationContext context) in c:\\TeamCity\\buildAgent\\work\\8567e2ba106d3992\\Source\\Glass.Mapper\\Configuration\\AbstractTypeConfiguration.cs:line 151\r\n   at Glass.Mapper.Pipelines.ObjectConstruction.Tasks.CreateConcrete.CreateConcreteTask.CreateObject(ObjectConstructionArgs args) in c:\\TeamCity\\buildAgent\\work\\8567e2ba106d3992\\Source\\Glass.Mapper\\Pipelines\\ObjectConstruction\\Tasks\\CreateConcrete\\CreateConcreteTask.cs:line 104","InnerException":{"Message":"An error has occurred.","ExceptionMessage":"Failed to map property Hours on Models.Store","ExceptionType":"Glass.Mapper.MapperException","StackTrace":"   at Glass.Mapper.Configuration.AbstractTypeConfiguration.MapPropertiesToObject(Object obj, IAbstractService service, AbstractTypeCreationContext context) in c:\\TeamCity\\buildAgent\\work\\8567e2ba106d3992\\Source\\Glass.Mapper\\Configuration\\AbstractTypeConfiguration.cs:line 142","InnerException":{"Message":"An error has occurred.","ExceptionMessage":"Failed to map to property 'Hours' on type 'Models.Store'","ExceptionType":"Glass.Mapper.MapperException","StackTrace":"   at Glass.Mapper.AbstractDataMapper.MapCmsToProperty(AbstractDataMappingContext mappingContext) in c:\\TeamCity\\buildAgent\\work\\8567e2ba106d3992\\Source\\Glass.Mapper\\AbstractDataMapper.cs:line 64\r\n   at Glass.Mapper.Configuration.AbstractTypeConfiguration.MapPropertiesToObject(Object obj, IAbstractService service, AbstractTypeCreationContext context) in c:\\TeamCity\\buildAgent\\work\\8567e2ba106d3992\\Source\\Glass.Mapper\\Configuration\\AbstractTypeConfiguration.cs:line 121","InnerException":{"Message":"An error has occurred.","ExceptionMessage":"Object reference not set to an instance of an object.","ExceptionType":"System.NullReferenceException","StackTrace":"   at Glass.Mapper.Sc.DataMappers.SitecoreFieldStringMapper.RunPipeline(Field field) in c:\\TeamCity\\buildAgent\\work\\8567e2ba106d3992\\Source\\Glass.Mapper.Sc\\DataMappers\\SitecoreFieldStringMapper.cs:line 92\r\n   at Glass.Mapper.Sc.DataMappers.SitecoreFieldStringMapper.GetField(Field field, SitecoreFieldConfiguration config, SitecoreDataMappingContext context) in c:\\TeamCity\\buildAgent\\work\\8567e2ba106d3992\\Source\\Glass.Mapper.Sc\\DataMappers\\SitecoreFieldStringMapper.cs:line 87\r\n   at Glass.Mapper.AbstractDataMapper.MapCmsToProperty(AbstractDataMappingContext mappingContext) in c:\\TeamCity\\buildAgent\\work\\8567e2ba106d3992\\Source\\Glass.Mapper\\AbstractDataMapper.cs:line 60"}}}}}}

The only change made lately was the Hours field was changed from a single-line text to a rich-text field. Code-wise I wouldn't think this would matter, since the field is a string type when you declare the model in both cases.

The code we're using that's throwing the error is this:

   public List<Store> GetStores() {
        using (IProviderSearchContext context = ContentSearchManager.GetIndex("sitecore_web_index").CreateSearchContext()) {
            ID storeTemplateId = new ID(Store.ModelTemplateId);
            IQueryable<SearchResultItem> query = context.GetQueryable<SearchResultItem>().Where(i => i.TemplateId == storeTemplateId);
            SearchResults<SearchResultItem> results = query.Filter(y => y.Language == Sitecore.Context.Language.Name).GetResults();

            if (!results.Hits.Any()) {
                return new List<Store>();
            }

            ISitecoreService masterService = new SitecoreService("web");
            ConcurrentBag<Store> stores = new ConcurrentBag<Store>();
            Parallel.ForEach(results, result => stores.Add(masterService.Cast<Store>(result.Document.GetItem())));

            return stores.Where(x => x != null).ToList();
        }
    }

The error occurs on the Parallel.ForEach line, but I've deconstructed that back to a standard foreach loop and it's the .Cast<Store> area that's causing it. I also changed the code to take the item ID from search and use a .GetItem<Store>([ID]) but I got the exact same error. I've confirmed the property is marked virtual in the model as well...it has a SitecoreField attribute on it, but doesn't need it being a single word field name.

I've republished the entire site, republished by sections, rebuilt the index, with no luck. My only other recourse right now might be to dump the sitecore_web_index files and rebuild from ground zero, or to wipe out my web database and create an empty to do a republish to (or both). But I'm hoping someone might have an insight I'm missing. The lack of reproducibility across all environments gives me hope that it is, perhaps, just my environment, but I'm understandably nervous about pushing code to production that could knock out an important client function.

EDIT:

Per alexp83's suggestion, I took the code down to a direct item retrieval and then a cast:

Item item = Database.GetDatabase("web").GetItem(new ID("{B1AFA586-58D9-498D-95EE-FA4B6A0FA7F7}"));
Store store = masterService.Cast<Store>(item);
stores.Add(store);

And I got the same error on the cast line. For Michael Edwards, I was trying to just pull what I thought was relevant, but this is the entire stack trace I get back for the test code I wrote above:

{"Message":"An error has occurred.","ExceptionMessage":"Failed to create type Models.Store","ExceptionType":"Glass.Mapper.MapperException","StackTrace":"   at Glass.Mapper.Pipelines.ObjectConstruction.Tasks.CreateConcrete.CreateConcreteTask.CreateObject(ObjectConstructionArgs args) in c:\\TeamCity\\buildAgent\\work\\8567e2ba106d3992\\Source\\Glass.Mapper\\Pipelines\\ObjectConstruction\\Tasks\\CreateConcrete\\CreateConcreteTask.cs:line 115\r\n   at Glass.Mapper.Pipelines.ObjectConstruction.Tasks.CreateConcrete.CreateConcreteTask.Execute(ObjectConstructionArgs args) in c:\\TeamCity\\buildAgent\\work\\8567e2ba106d3992\\Source\\Glass.Mapper\\Pipelines\\ObjectConstruction\\Tasks\\CreateConcrete\\CreateConcreteTask.cs:line 68\r\n   at Glass.Mapper.Pipelines.AbstractPipelineRunner`2.<>c__DisplayClass3.<CreateTaskExpression>b__2(T args) in c:\\TeamCity\\buildAgent\\work\\8567e2ba106d3992\\Source\\Glass.Mapper\\Pipelines\\AbstractPipelineRunner.cs:line 77\r\n   at Glass.Mapper.Pipelines.AbstractPipelineRunner`2.<>c__DisplayClass3.<CreateTaskExpression>b__2(T args) in c:\\TeamCity\\buildAgent\\work\\8567e2ba106d3992\\Source\\Glass.Mapper\\Pipelines\\AbstractPipelineRunner.cs:line 82\r\n   at Glass.Mapper.Pipelines.AbstractPipelineRunner`2.<>c__DisplayClass3.<CreateTaskExpression>b__2(T args) in c:\\TeamCity\\buildAgent\\work\\8567e2ba106d3992\\Source\\Glass.Mapper\\Pipelines\\AbstractPipelineRunner.cs:line 82\r\n   at Glass.Mapper.Pipelines.AbstractPipelineRunner`2.<>c__DisplayClass3.<CreateTaskExpression>b__2(T args) in c:\\TeamCity\\buildAgent\\work\\8567e2ba106d3992\\Source\\Glass.Mapper\\Pipelines\\AbstractPipelineRunner.cs:line 82\r\n   at Glass.Mapper.Pipelines.AbstractPipelineRunner`2.<>c__DisplayClass3.<CreateTaskExpression>b__2(T args) in c:\\TeamCity\\buildAgent\\work\\8567e2ba106d3992\\Source\\Glass.Mapper\\Pipelines\\AbstractPipelineRunner.cs:line 82\r\n   at Glass.Mapper.Pipelines.AbstractPipelineRunner`2.<>c__DisplayClass3.<CreateTaskExpression>b__2(T args) in c:\\TeamCity\\buildAgent\\work\\8567e2ba106d3992\\Source\\Glass.Mapper\\Pipelines\\AbstractPipelineRunner.cs:line 82\r\n   at Glass.Mapper.AbstractService.InstantiateObject(AbstractTypeCreationContext abstractTypeCreationContext) in c:\\TeamCity\\buildAgent\\work\\8567e2ba106d3992\\Source\\Glass.Mapper\\AbstractService.cs:line 139\r\n   at Glass.Mapper.Sc.SitecoreService.CreateType(Type type, Item item, Boolean isLazy, Boolean inferType, Dictionary`2 parameters, Object[] constructorParameters) in c:\\TeamCity\\buildAgent\\work\\8567e2ba106d3992\\Source\\Glass.Mapper.Sc\\SitecoreService.cs:line 506\r\n   at Glass.Mapper.Sc.SitecoreService.CreateType[T](Item item, Boolean isLazy, Boolean inferType, Object[] constructorParameters) in c:\\TeamCity\\buildAgent\\work\\8567e2ba106d3992\\Source\\Glass.Mapper.Sc\\SitecoreService.cs:line 484\r\n   at Glass.Mapper.Sc.SitecoreService.CreateType[T](Item item, Boolean isLazy, Boolean inferType) in c:\\TeamCity\\buildAgent\\work\\8567e2ba106d3992\\Source\\Glass.Mapper.Sc\\SitecoreService.cs:line 362\r\n   at Repositories.Cms.LocationRepository.GetStores() in C:\\Code\\FiveGuys\\Source\\PublicSite\\Repositories.Cms\\LocationRepository.cs:line 43\r\n   at Services.LocationService.GetAllStores() in C:\\Code\\FiveGuys\\Source\\PublicSite\\Services\\LocationService.cs:line 84\r\n   at Services.LocationService.GetStoreByDistance(Double lat, Double lng, Int32 distance, Units units) in C:\\Code\\FiveGuys\\Source\\PublicSite\\Services\\LocationService.cs:line 36\r\n   at PublicSite.Web.ApiControllers.StoresController.ByDistance(Double lat, Double lng, Int32 distance, Int32 secondaryDistance, String units) in C:\\Code\\FiveGuys\\Source\\PublicSite\\PublicSite.Web\\ApiControllers\\StoresController.cs:line 29\r\n   at lambda_method(Closure , Object , Object[] )\r\n   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters)\r\n   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Filters.ActionFilterAttribute.<CallOnActionExecutedAsync>d__5.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Web.Http.Filters.ActionFilterAttribute.<CallOnActionExecutedAsync>d__5.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Filters.ActionFilterAttribute.<ExecuteActionFilterAsyncCore>d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Filters.ActionFilterAttribute.<CallOnActionExecutedAsync>d__5.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Web.Http.Filters.ActionFilterAttribute.<CallOnActionExecutedAsync>d__5.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Filters.ActionFilterAttribute.<ExecuteActionFilterAsyncCore>d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Filters.AuthorizationFilterAttribute.<ExecuteAuthorizationFilterAsyncCore>d__2.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Controllers.ExceptionFilterResult.<ExecuteAsync>d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Web.Http.Controllers.ExceptionFilterResult.<ExecuteAsync>d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()","InnerException":{"Message":"An error has occurred.","ExceptionMessage":"Failed to map properties on /sitecore/content/Global Resources/Locations/Canada/Quebec/CLOSED 22013--1482.","ExceptionType":"Glass.Mapper.MapperException","StackTrace":"   at Glass.Mapper.Configuration.AbstractTypeConfiguration.MapPropertiesToObject(Object obj, IAbstractService service, AbstractTypeCreationContext context) in c:\\TeamCity\\buildAgent\\work\\8567e2ba106d3992\\Source\\Glass.Mapper\\Configuration\\AbstractTypeConfiguration.cs:line 151\r\n   at Glass.Mapper.Pipelines.ObjectConstruction.Tasks.CreateConcrete.CreateConcreteTask.CreateObject(ObjectConstructionArgs args) in c:\\TeamCity\\buildAgent\\work\\8567e2ba106d3992\\Source\\Glass.Mapper\\Pipelines\\ObjectConstruction\\Tasks\\CreateConcrete\\CreateConcreteTask.cs:line 104","InnerException":{"Message":"An error has occurred.","ExceptionMessage":"Failed to map property Hours on Models.Store","ExceptionType":"Glass.Mapper.MapperException","StackTrace":"   at Glass.Mapper.Configuration.AbstractTypeConfiguration.MapPropertiesToObject(Object obj, IAbstractService service, AbstractTypeCreationContext context) in c:\\TeamCity\\buildAgent\\work\\8567e2ba106d3992\\Source\\Glass.Mapper\\Configuration\\AbstractTypeConfiguration.cs:line 142","InnerException":{"Message":"An error has occurred.","ExceptionMessage":"Failed to map to property 'Hours' on type 'Models.Store'","ExceptionType":"Glass.Mapper.MapperException","StackTrace":"   at Glass.Mapper.AbstractDataMapper.MapCmsToProperty(AbstractDataMappingContext mappingContext) in c:\\TeamCity\\buildAgent\\work\\8567e2ba106d3992\\Source\\Glass.Mapper\\AbstractDataMapper.cs:line 64\r\n   at Glass.Mapper.Configuration.AbstractTypeConfiguration.MapPropertiesToObject(Object obj, IAbstractService service, AbstractTypeCreationContext context) in c:\\TeamCity\\buildAgent\\work\\8567e2ba106d3992\\Source\\Glass.Mapper\\Configuration\\AbstractTypeConfiguration.cs:line 121","InnerException":{"Message":"An error has occurred.","ExceptionMessage":"Object reference not set to an instance of an object.","ExceptionType":"System.NullReferenceException","StackTrace":"   at Glass.Mapper.Sc.DataMappers.SitecoreFieldStringMapper.RunPipeline(Field field) in c:\\TeamCity\\buildAgent\\work\\8567e2ba106d3992\\Source\\Glass.Mapper.Sc\\DataMappers\\SitecoreFieldStringMapper.cs:line 92\r\n   at Glass.Mapper.Sc.DataMappers.SitecoreFieldStringMapper.GetField(Field field, SitecoreFieldConfiguration config, SitecoreDataMappingContext context) in c:\\TeamCity\\buildAgent\\work\\8567e2ba106d3992\\Source\\Glass.Mapper.Sc\\DataMappers\\SitecoreFieldStringMapper.cs:line 87\r\n   at Glass.Mapper.AbstractDataMapper.MapCmsToProperty(AbstractDataMappingContext mappingContext) in c:\\TeamCity\\buildAgent\\work\\8567e2ba106d3992\\Source\\Glass.Mapper\\AbstractDataMapper.cs:line 60"}}}}}
7
  • What happens when you call Database.GetDatabase("web").GetItem([ID]) with ID that's erroring out?
    – alexp83
    Jun 19, 2017 at 15:52
  • 1
    As a side note; it's an error/misconception that anything found in the index will always be found as an item. You should always code defensively, so that an index slightly out of sync does not take your application down. Further; consider not needing to get the item at all. Jun 19, 2017 at 15:57
  • Do you have the top level exception message? It feels like the stack/info is incomplete. Jun 19, 2017 at 16:16
  • I've updated the original item to answer questions. Jun 19, 2017 at 16:23
  • Is it a rich text field on a background thread? Jun 19, 2017 at 16:55

1 Answer 1

4

I found a reproducible answer to the problem, and I think it's because the functions involved were running via an API controller. If I made the field a single-line text field, the function worked correctly, but when I switched back to a rich text field, the error occurred again. (It also worked correctly if I removed the field from the model, no other field had an issue.) I added the Setting = SitecoreFieldSettings.RichTextRaw attribute to the property in the model, and my code worked correctly after that.

1
  • Thank you! Had this problem all day and I was kicking myself because I couldn't figure it out. Finally tonight, I found out that all the fields erroring were Rich Text fields. I'm thinking a article needs to be written about this issue. I'm also working on an API controller method. Mar 17, 2020 at 2:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.