Umbraco 8.1.1: 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 IUmbracoContextFactory _context;
public ContentProvider(IUmbracoContextFactory context)
{
_context = context;
}
public IPublishedContent GetHomeNode()
{
using (var contextResource = _context.EnsureUmbracoContext().UmbracoContext)
{
return contextResource.Content.GetSingleByXPath("//Home");
}
}
}
But I keep getting
Cannot access a disposed object. Object name 'snapshot'
Update:
When using a using-statement, it disposes everything. When you try to get the .Url or .Parent or something similar, it throws the not exposed error
The problem is indeed that you put the UmbracoContext in a using, but you should put the UmbracoContextReference in a using. Just like what Bjarne said.
The tricky part is this should be done throughout your entire application. Just fixing the lines that you shared here does not fix it. You will be getting the same exception. Fixing it in every case in your solution (or at least in all the cases inside your call stack that lead to your shared lines of code) is the only way resolve this.
Where _context is a call to the IUmbracoContextFatcory
using(var context = _context.EnsureUmbracoContext()) {
//use context for all umbraco calls
}
One pitfall I had when using the above was that I had one part accessing another func which in itself used a contextfactory to get the context, and if the outer function was killed by a bug, the inner ensurecontext was somehow killed permanently.
Umbraco 8.1.1: Cannot access a disposed object. Object name 'snapshot'
Hi,
I am trying to use an UmbracoContext in a custom service (via DI):
But I keep getting
Update: When using a using-statement, it disposes everything. When you try to get the .Url or .Parent or something similar, it throws the not exposed error
It seems to be a little bug.
But for now, on some services, the fix/workaround is:
On others I keep getting that the context is disposed. Both in a using and in everything else.
Hi Sibren
Have you tried the following?
/Bjarne
I'm also getting this error. Someone already found a fix? Trying to get a Parent object gives me the snapshot error.
Hi Sibren,
The problem is indeed that you put the UmbracoContext in a using, but you should put the UmbracoContextReference in a using. Just like what Bjarne said.
The tricky part is this should be done throughout your entire application. Just fixing the lines that you shared here does not fix it. You will be getting the same exception. Fixing it in every case in your solution (or at least in all the cases inside your call stack that lead to your shared lines of code) is the only way resolve this.
Hope this helps,
Cheers,
Ruben
Where _context is a call to the
IUmbracoContextFatcory
One pitfall I had when using the above was that I had one part accessing another func which in itself used a contextfactory to get the context, and if the outer function was killed by a bug, the inner ensurecontext was somehow killed permanently.
is working on a reply...