Copied to clipboard

Flag this post as spam?

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


  • Sibren 22 posts 134 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 22 posts 134 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 1140 posts 3230 karma points MVP 3x 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

Please Sign in or register to post replies

Write your reply to:

Draft