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.

Umbraco.Content throws an ObjectDisposedException

See original GitHub issue

A call to Umbraco.Content leads to an exception:

[ObjectDisposedException: Cannot access a disposed object.
Object name: 'snapshot'.]
   Umbraco.Web.PublishedCache.NuCache.Snapshot.Get(Int32 id) in C:\Projekte\Umbraco-CMS\src\Umbraco.Web\PublishedCache\NuCache\ContentStore.cs:1131
   Umbraco.Web.PublishedCache.NuCache.ContentCache.GetById(Boolean preview, Int32 contentId) in C:\Projekte\Umbraco-CMS\src\Umbraco.Web\PublishedCache\NuCache\ContentCache.cs:231
   Umbraco.Web.PublishedCache.PublishedCacheBase.GetById(Int32 contentId) in C:\Projekte\Umbraco-CMS\src\Umbraco.Web\PublishedCache\PublishedCacheBase.cs:23
   Umbraco.Web.PublishedContentQuery.ItemById(Int32 id, IPublishedCache cache) in C:\Projekte\Umbraco-CMS\src\Umbraco.Web\PublishedContentQuery.cs:123
   Umbraco.Web.PublishedContentQuery.Content(Int32 id) in C:\Projekte\Umbraco-CMS\src\Umbraco.Web\PublishedContentQuery.cs:37
   Umbraco.Web.UmbracoHelper.Content(Int32 id) in C:\Projekte\Umbraco-CMS\src\Umbraco.Web\UmbracoHelper.cs:326

Reproduction

Unfortunately, I can’t create a simple System that helps reproducing the Bug. The Code in my System comes down to the following Steps:

In my master layout Master.cshmtl I call a Method in a helper class like that: <meta name="description" content=@MyHelperClass.GetDescription()>

The GetDescription() Method in the helper class determines a content like that:

pc = Umbraco.Content( (int) HttpContext.Current.Items["CurrentPage"] );

In the specified case the parameter to Umbraco.Content is exactly the Id of the content, which is about to be displayed using the master.cshtml. So it’s definitely published and it should be possible to render the content.

The exception occurs in Snapshot.Get(int id) in ContentStore.cs:

public ContentNode Get(int id)
{
    if (_gen < 0)
        throw new ObjectDisposedException("snapshot" /*+ " (" + _thisCount + ")"*/);
    return _store.Get(id, _gen);
}

The exception is thrown because _gen < 0.

Expected result

Umbraco.Content should return the IPublishedContent with the given Id

Actual result

The exception is thrown.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
Shazwazzacommented, Mar 26, 2019

I also highly recommend to stop using statics and stop using singletons. These are the cause for so many issues and also for spaghetti code. If you require a custom helper in your views, best to create a new helper with it’s dependencies specified in the constructor when you need it. Or, If you require your custom helper in your controllers, then it’s best to register your helper in dependency injection container and add this dependency to your controllers constructor.

1reaction
Shazwazzacommented, Mar 26, 2019

I very highly recommend reading this document https://our.umbraco.com/documentation/Reference/Common-Pitfalls/. You are not the first person to do this and this circumstance also exists in v7 but doesn’t error out but instead will cause your app to behave in odd ways or throw null ref exceptions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

triggering index rebuild via hangfire causes ...
UmbracoContext.Content.GetById(id);. Except, the line setting documentStub is throwing an ObjectDisposedException. What's even more unusual ...
Read more >
Cannot access a disposed object. Object name 'snapshot'
Hi,. I am trying to use an UmbracoContext in a custom service (via DI): public class ContentProvider : IContentProvider { private readonly ...
Read more >
The CancellationTokenSource has been disposed ...
AggregateException: Exceptions were thrown by listed actions. ---> System.ObjectDisposedException: The CancellationTokenSource has been ...
Read more >
error when publishing document - Contentment Feedback
I have a document type with a block editor and a few Contnetment content pickers with umbraco source as the data type and...
Read more >
unhandled controller exception occurred for request
Anybody run into this? /umbraco/backoffice/UmbracoApi/Content/PostSave. System.AggregateException: Exceptions were thrown by listed actions.
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