In a legacy project, there's code that retrieves items like this:
var context = SitecoreContext.GetFromHttpContext();
context.GetItem<Item>(itemId, Language.Current);
For several types of items, this results in a NullReferenceException
with the following stack trace:
Glass.Mapper.AbstractService.RunConfigurationPipeline(AbstractTypeCreationContext abstractTypeCreationContext)
Glass.Mapper.AbstractService.InstantiateObject(AbstractTypeCreationContext abstractTypeCreationContext)
Glass.Mapper.Sc.SitecoreService.CreateType(Type type, Item item, Boolean isLazy, Boolean inferType, Dictionary`2 parameters, Object[] constructorParameters)
Glass.Mapper.Sc.SitecoreService.CreateType(Item item, Boolean isLazy, Boolean inferType, Object[] constructorParameters)
Glass.Mapper.Sc.SitecoreService.CreateType(Item item, Boolean isLazy, Boolean inferType)
... soluction code here ...
According to the registered Glass issues I have found, this happens when caching is enabled and referenced item collections are loaded lazily:
https://github.com/mikeedwards83/Glass.Mapper/issues/169 https://github.com/mikeedwards83/Glass.Mapper/issues/247
We use TDS code generation to create Glass models.
Caching is enabled globally in Glass.Mapper.Sc.config
:
<setting name="Glass.Mapper.Sc.Caching.Enabled" value="true"/>
We cannot disable caching, as it improves performance. But I think it wouldn't affect us much if we disabled lazy loading.
It looks like lazy loading is enabled by default for all models, unless specified otherwise with Glass model attributes.
Is is possible to disable lazy loading globally? Or is there a better way to solve the errors we're getting?
Glass version is 4.0.5.54.