Copied to clipboard

Flag this post as spam?

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


  • Sibren 28 posts 170 karma points
    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 28 posts 170 karma points
    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 1179 posts 3435 karma points MVP 4x c-trib
    Aug 06, 2019 @ 12:03
    Bjarne Fyrstenborg
    0

    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
    0

    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

Please Sign in or register to post replies

Write your reply to:

Draft