Umbraco.Content throws an ObjectDisposedException
See original GitHub issueA 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:
- Created 4 years ago
- Comments:7 (4 by maintainers)
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.
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.