question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using LazyItemEnumerable being used even with presence of Cachable=true & IsLazy = false

See original GitHub issue

I’m seeing errors related to a disposed SitecoreContext trying to be used, this would make sense if I were trying to access a cached item that has lazily loaded properties but I’ve made every attempt to disable lazy loading in the solution. I’ve appended [SitecoreType(Cachable = true, AutoMap = true)] to every model in the project and [SitecoreChildren(IsLazy = false, InferType = true)] to every IEnumerable property and yet I can see in the errors that Glass.Mapper.Sc.LazyItemEnumerable`1.ProcessItems() is still being used, which then is cached resulting in the SitecoreContext going out of scope.

I’ve tried to follow all the instructions on the GlassMapper website to disable LazyLoading, could you please advise how I can prevent the solution using LazyLoading?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
ted-duncancommented, Jul 6, 2019

whatever became of looking next week?

2reactions
Glass-Build-Githubcommented, Nov 10, 2018

Hi

Thanks for sending the test case. I will take a look next week.

Mike

On Sat, 10 Nov 2018, 20:35 Niclas Leon Bock <notifications@github.com wrote:

Hey there,

we’ve been able to further narrow down the specific issue.

We’re able to reliably reproduce the issue with the the following test case, which is based on the test cases proposed/provided by you:

[Test]public void SitecoreService_NotLazyLoadingChildrenOfReferencedItem_EnumerationAfterServiceDisposal() { //Arrange using (Db db = new Db() { new DbItem(“Root”) { new DbField(“Reference”) { Value = “{564F2C6A-C171-4630-8A94-7A9749C00975}” } }, new DbItem(“Target”, new ID(“{564F2C6A-C171-4630-8A94-7A9749C00975}”)) { new DbItem(“Child1”), new DbItem(“Child2”) } }) {

    var resolver = Utilities.CreateStandardResolver();
    var context = Context.Create(resolver);
    var service = new SitecoreService(db.Database, context);

    //Act
    var result = service.GetItem<IStub>("/sitecore/content/Root");

    service.Dispose();

    //Assert
    Assert.AreEqual(true, result.Reference.Children.Any());
    Assert.AreEqual(12, result.Reference.Children.Count());
}

}

Using the following stubs:

public interface IStub {

IStubNotLazy Reference { get; set; }

} public interface IStubNotLazy {

[SitecoreChildren(IsLazy = false)]
IEnumerable<IStubNotLazy> Children { get; set; }

Guid Id { get; set; }

}

The test fails with the exception under discussion:

Glass.Mapper.MapperException : Service has been disposed, cannot create object at Glass.Mapper.AbstractService.InstantiateObject(AbstractTypeCreationContext abstractTypeCreationContext) in C:\Dev\Glass.Mapper\Source\Glass.Mapper\AbstractService.cs:line 142 at Glass.Mapper.Sc.SitecoreService.CreateType(Type type, Item item, Boolean isLazy, Boolean inferType, Dictionary2 parameters, Object[] constructorParameters) in C:\Dev\Glass.Mapper\Source\Glass.Mapper.Sc\SitecoreService.cs:line 501 at Glass.Mapper.Sc.LazyItemEnumerable1.ProcessItems() in C:\Dev\Glass.Mapper\Source\Glass.Mapper.Sc\LazyItemEnumerable.cs:line 95 at Glass.Mapper.Sc.LazyItemEnumerable1.<.ctor>b__6_0() in C:\Dev\Glass.Mapper\Source\Glass.Mapper.Sc\LazyItemEnumerable.cs:line 61 at System.Lazy1.CreateValue() at System.Lazy1.LazyInitValue() at Glass.Mapper.Sc.LazyItemEnumerable1…ctor(Func`1 getItems, Boolean isLazy, Boolean inferType, ISitecoreService service) in C:\Dev\Glass.Mapper\Source\Glass.Mapper.Sc\LazyItemEnumerable.cs:line 66 at lambda_method(Closure , Object[] ) at Glass.Mapper.Sc.DataMappers.SitecoreChildrenMapper.MapToProperty(AbstractDataMappingContext mappingContext) in C:\Dev\Glass.Mapper\Source\Glass.Mapper.Sc\DataMappers\SitecoreChildrenMapper.cs:line 86 at Glass.Mapper.Pipelines.ObjectConstruction.Tasks.CreateInterface.InterfacePropertyInterceptor.LoadValue(AbstractPropertyConfiguration propertyConfiguration) in C:\Dev\Glass.Mapper\Source\Glass.Mapper\Pipelines\ObjectConstruction\Tasks\CreateInterface\InterfacePropertyInterceptor.cs:line 137 at Glass.Mapper.Pipelines.ObjectConstruction.Tasks.CreateInterface.InterfacePropertyInterceptor.Intercept(IInvocation invocation) in C:\Dev\Glass.Mapper\Source\Glass.Mapper\Pipelines\ObjectConstruction\Tasks\CreateInterface\InterfacePropertyInterceptor.cs:line 102 at Castle.DynamicProxy.AbstractInvocation.Proceed() at Castle.Proxies.IStubNotLazyProxy.get_Children() at Glass.Mapper.Sc.FakeDb.Issues.Issue299.Issue299Fixture.SitecoreService_NotLazyLoadingChildrenOfReferencedItem_EnumerationAfterServiceDisposal() in C:\Dev\Glass.Mapper\Tests\Unit Tests\Glass.Mapper.Sc.FakeDb\Issues\Issue299\Issue299Fixture.cs:line 49

As you can see, the issue only occurs when referencing from an item. It seems to be related to using lists/enumerables contained in typed referenced items. Simply accessing referenced items after service disposal works. To proof this, we adjusted the test case accordingly:

using (Db db = new Db() { new DbItem(“Root”) { new DbField(“Reference”) { Value = “{564F2C6A-C171-4630-8A94-7A9749C00975}” } }, new DbItem(“Target”, new ID(“{564F2C6A-C171-4630-8A94-7A9749C00975}”)) { new DbItem(“Child1”), new DbItem(“Child2”) } }) {

    var resolver = Utilities.CreateStandardResolver();
    var context = Context.Create(resolver);
    var service = new SitecoreService(db.Database, context);

    //Act
    var result = service.GetItem<IStub>("/sitecore/content/Root");

    service.Dispose();

    //Assert
    Assert.NotNull(result.Reference);
    Assert.AreEqual(result.Reference.Id, new Guid("{564F2C6A-C171-4630-8A94-7A9749C00975}"));
}

}

This results in the test being green again.

We’re running the suite with Sitecore 8.2 and Glass.Mapper on tag build-4.5.0.4-beta as that’s the version currently in use.

For now, we’ll use Guids in the “Root” model instead of the typed referenced models and fetch and cast the item manually. However, we’ll still be looking into the issue in more detail and are hoping to be able to resolve it soon.

We’re happy for any help or hints. Please let us know, if you have any “bright idea” where the issue may come from or need further information from us.

Best regards

— 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/299#issuecomment-437619377, or mute the thread https://github.com/notifications/unsubscribe-auth/AT1mzoh6h_voeht8Yl19ZEpaHZdNIdKGks5utziZgaJpZM4NY_jL .

Read more comments on GitHub >

github_iconTop Results From Across the Web

Developers - Using LazyItemEnumerable being used even ...
I've appended [SitecoreType(Cachable = true, AutoMap = true)] to every model in the project and [SitecoreChildren(IsLazy = false, InferType = ...
Read more >
Is it possible to cache IEnumerable lazy evaluation results?
Yes there is. If the Enumerator returned by your Lazy call gets its data in turn from the same instance of an underlying...
Read more >
C#: IEnumerable, yield return, and lazy evaluation
A perfect scenario for a simple F# script - I'll use type providers and have the entire thing type safe and done in...
Read more >
C#: IEnumerable, yield return, and lazy evaluation
An iterator is a method that uses the yield return keywords. yield return is different from ... the value of didTheCodeRun is still...
Read more >
IEnumerable is Lazy… And That's Cool
IEnumerable is lazy while an array or list is implicitly eager. Consider an array. First, you have to define its size which is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found