Glass.Mapper
Glass.Mapper copied to clipboard
NullReferenceException Sitecore 9.3 build
Hi, we have encountered null exceptions, which I think is related to Sitecore context being lost on Singleton lifecycle, and currently it is only misbehaving on CM, and requires restarting CM.
Might be related to these posts, but I would've thought the issue would've been addressed in latest version. https://github.com/mikeedwards83/Glass.Mapper/issues/340 https://github.com/mikeedwards83/Glass.Mapper/issues/374
Version: Glass.Mapper.Sc.93 (5.6.160)
Exception: System.NullReferenceException Message: Object reference not set to an instance of an object. Source: Glass.Mapper.Sc at Glass.Mapper.Sc.GetItemByIdOptions.GetItem(Database database) in C:\TeamCity\buildAgent\work\9693a2d38f55a4a\Source\Glass.Mapper.Sc:line 51 at Glass.Mapper.Sc.SitecoreService.GetItem(GetItemOptions options) in C:\TeamCity\buildAgent\work\9693a2d38f55a4a\Source\Glass.Mapper.Sc\SitecoreService.cs:line 336 at Glass.Mapper.Sc.SitecoreService.GetItem[T](GetItemOptions options) in C:\TeamCity\buildAgent\work\9693a2d38f55a4a\Source\Glass.Mapper.Sc\SitecoreService.cs:line 320
Code, serviceCollection.AddSingleton<Func<Database, ISitecoreService>>(_ => CreateSitecoreService); serviceCollection.AddScoped(_ => CreateSitecoreContextService()); serviceCollection.AddScoped(_ => CreateRequestContext()); serviceCollection.AddScoped(_ => CreateGlassHtml()); serviceCollection.AddScoped(_ => CreateMvcContext()); serviceCollection.AddScoped(_ => CreateWebFormsContext());
Are one of the scoped instances disposing the singleton instance?
Hi Mike,
We're experienced this very exact issue with Glass Mapper 5.8.170 on Sitecore 9.3.0. Are you aware of any workarounds or estimate of when this will be fixed?
Thank you in advance, Mihaela.
This is probably a disposal of the SitecoreService before it should have been. You need to check the lifecycle of the object and see if you have anything disposing the service incorrectly. Check your IOC registrations as well..
Hi. We are experiencing this now as well on a Sitecore 9.3 installation running Glass.Mapper.Sc.93 (5.6.160). It only happens on CM for us as well and a restart solves it. There is no continuity, it just happens randomly, often over the weekend when no-one has been working in the environment, but has happened mid week once.
@webbson You need check if you have any service registered as Singleton and check if they are depending on ISitecoreService (or any service which uses ISitecoreService, e.g IRequestContext etc) I had exactly the same issue on Sitecore V10.1. (and I had a singleton service, using IRequestContext, which uses ISitecoreService)