Glass.Mapper
Glass.Mapper copied to clipboard
Glass.Mapper.MapperException: Failed to map field occurs
Hello,
We often encounter "Glass.Mapper.MapperException: Failed to map field occurs" exceptions on our test and acceptance environments (never on dev). These issues are very similair to this and this issue.
All our models are interfaces and cacheable. After a republish of the item that cannot be found, it works again, but this is often temporarily and the error quickly reoccurs. We use IoC, and the lifecycle of the SitecoreContext is very long. TST and ACC are on Glass version 4.2.1.188.
Below is an example of our logs (I've redacted implementation specific details)
Exception: Glass.Mapper.MapperException Message: Failed to map field Fieldname with value {275CDD1C-5577-4F92-9721-6D7C3203DDFB} Source: Glass.Mapper.Sc at Glass.Mapper.Sc.DataMappers.AbstractSitecoreFieldMapper.GetField(Field field, SitecoreFieldConfiguration config, SitecoreDataMappingContext context) at Glass.Mapper.Pipelines.ObjectConstruction.Tasks.CreateInterface.InterfacePropertyInterceptor.LoadValue(AbstractPropertyConfiguration propertyConfiguration) at Glass.Mapper.Pipelines.ObjectConstruction.Tasks.CreateInterface.InterfacePropertyInterceptor.Intercept(IInvocation invocation) at Castle.DynamicProxy.AbstractInvocation.Proceed() at Castle.Proxies.IProjectnameModelProxy.get_Modelname()
It seems to be related to caching - it's always the same fields that cannot be retrieved, but disabling caching also disables this issue, which makes it seem like it's related to this and this issue.
I'm going to try and disable LazyLoading on the specific fields later this week, and will report my findings..
Can you give me an example of the model and how it is used?
Maybe I can reproduce this issue.
Thanks
On 22 May 2017 at 14:09, Zwemvest [email protected] wrote:
It seems to be related to caching - it's always the same fields that cannot be retrieved, but disabling caching also disables this issue, which makes it seem like it's related to this https://github.com/mikeedwards83/Glass.Mapper/issues/169 issue.
I'm going to try and disable LazyLoading on the specific fields later this week, and will report my findings..
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/mikeedwards83/Glass.Mapper/issues/304#issuecomment-303095062, or mute the thread https://github.com/notifications/unsubscribe-auth/AAHj0ClAXs5AMAJqajHtUvCvtVKft5xvks5r8YjugaJpZM4NiGtR .
Hi mr Edwards, here's a copy of the code that isn't working. These are the models;
public interface IItemModel
{
[SitecoreId]
Guid Id { get; set; }
[SitecoreInfo(SitecoreInfoType.Name)]
string Name { get; set; }
[SitecoreInfo(SitecoreInfoType.DisplayName)]
string DisplayName { get; set; }
[SitecoreInfo(SitecoreInfoType.Url)]
string Url { get; set; }
[SitecoreInfo(SitecoreInfoType.Language)]
Language Language { get; set; }
[SitecoreInfo(SitecoreInfoType.Version)]
int Version { get; set; }
[SitecoreInfo(SitecoreInfoType.TemplateId)]
Guid TemplateId { get; set; }
[SitecoreInfo(SitecoreInfoType.TemplateName)]
string TemplateName { get; set; }
[SitecoreInfo(SitecoreInfoType.Path)]
string Path { get; set; }
string FullPath { get; set; }
DateTime Created { get; set; }
DateTime Updated { get; set; }
int SortOrder { get; set; }
IItemModel Parent { get; set; }
IEnumerable<IItemModel> Children { get; set; }
}
[SitecoreType(TemplateId = "{12E790E5-3A0F-4F29-B5F6-84A2A1C3FA98}", AutoMap = true, Cachable = true)]
public interface IImageBaseModel : IItemModel
{
[SitecoreField("Show Alt Text")]
bool ShowAltText { get; set; }
[SitecoreField("Large viewport image")]
Image LargeViewportImage { get; set; }
[SitecoreField("Small viewport image")]
Image SmallViewportImage { get; set; }
}
[SitecoreType(TemplateId = "{AEF65A85-EBA3-4E6F-ABEA-668FA37B23BD}", AutoMap = true, Cachable = true)]
public interface IContactFooterModel : IImageBaseModel
{
[SitecoreField]
string ContactFooterTitle { get; set; }
[SitecoreField]
string PhoneNumber { get; set; }
[SitecoreField]
string PhoneNumberCostsLabel { get; set; }
[SitecoreField]
string ContactFooterText { get; set; }
[SitecoreField]
IEnumerable<ILocationModel> Locations { get; set; }
[SitecoreField("More Information")]
Link MoreInformation { get; set; }
}
[SitecoreType(TemplateId = "{2E8C6AFB-2403-4B09-A0D1-508AA597F187}", AutoMap = true, Cachable = true)]
public interface ILocationModel : IItemModel
{
[SitecoreField]
ISettingModel LocationType { get; set; }
[SitecoreField]
string LocationTitle { get; set; }
[SitecoreField]
string Street { get; set; }
[SitecoreField]
int StreetNumber { get; set; }
[SitecoreField]
string PostalCode { get; set; }
[SitecoreField]
string City { get; set; }
[SitecoreField("Postbus PostalCode City")]
string PostbusPostalCodeCity { get; set; }
[SitecoreField]
string Phonenumber { get; set; }
[SitecoreField]
double Latitude { get; set; }
[SitecoreField]
double Longitude { get; set; }
}
And here's an example of the code that's not working:
public ContactFooterViewModel(IContactFooterModel item, IImageManager imageManager) : base(item)
{
Locations = new List<ILocationModel>();
if (item?.Locations == null || !item.Locations.Any()) return;
Locations.AddRange(item.Locations);
Image = imageManager.ToViewModelImage(item);
}
The second check (!item.Locations.Any()) sometimes crashes. I presume that this has to do with LazyLoading. The check sometimes still throws Glass Mapper exceptions if we remove the LazyLoading attribute, but since enabling LazyLoading is the default setting, that shouldn't really matter anyways.
We haven't tried it with disabling LazyLoading yet, but removing the Cachable setting stops Glass Mapper exceptions from bubbling up, so that's why I related it to the other two issues I linked before.
Since we inherit from a base model, and you explained at SUGCON last friday that this isn't really a good way to build models, we want to keep the Cachable setting on our models. This would be to prevent Glass Mapper from mapping properties on the base model we don't need every time we retrieve a Glass Item.
However, it would be possible to disable LazyLoading on properties that cause errors. Obviously not on the base model, since that'd cause infinite loops of Parents referring to Children and Chilren referring to Parents.
Is this a correct train of thought? If so, I'll try to see if that works.