Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Sibren 40 posts 212 karma points c-trib
    Aug 06, 2019 @ 07:23
    Sibren
    0

    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

  • Sibren 40 posts 212 karma points c-trib
    Aug 06, 2019 @ 11:22
    Sibren
    0

    It seems to be a little bug.

    But for now, on some services, the fix/workaround is:

    public IPublishedContent GetHomeNode()
    {
        try
        {
            var contextResource = _context.EnsureUmbracoContext();
            return contextResource.UmbracoContext.Content.GetSingleByXPath("//Home");
        }
        catch (System.Exception exception)
        {
            return null;
        }
    }
    

    On others I keep getting that the context is disposed. Both in a using and in everything else.

  • Bjarne Fyrstenborg 1285 posts 4039 karma points MVP 8x c-trib
    Aug 06, 2019 @ 12:03
    Bjarne Fyrstenborg
    2

    Hi Sibren

    Have you tried the following?

    using (var cref = _context.EnsureUmbracoContext())
    {
         var node = cref.UmbracoContext.Content.GetSingleByXPath("//Home");
    
         return node;
    }
    

    /Bjarne

  • Nick 1 post 72 karma points
    May 11, 2020 @ 15:38
    Nick
    1

    I'm also getting this error. Someone already found a fix? Trying to get a Parent object gives me the snapshot error.

  • Ruben Meintema 9 posts 100 karma points
    May 13, 2020 @ 15:12
    Ruben Meintema
    100

    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

  • Damien Holley 180 posts 541 karma points
    Nov 30, 2021 @ 22:56
    Damien Holley
    0

    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.

Please Sign in or register to post replies

Write your reply to:

Draft