Copied to clipboard

Flag this post as spam?

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


  • Sibren 36 posts 188 karma points
    Sep 16, 2021 @ 13:35
    Sibren
    0

    EnsureUmbracoContext gives me a disposed UmbracoContext (from event)

    Hi,

    I've been breaking my head on this for some time now: From UI-O-Matic, we're firing an event on save, then we call a service to get the corresponding Umbraco-node. Because this is a background-thing, I get that there's no default UmbracoContext. I've read up on this topic: https://our.umbraco.com/forum/using-umbraco-and-getting-started/102676-triggering-index-rebuild-via-hangfire-causes-objectdisposedexception-in-nucache#comment-321129 and updated Umbraco to 8.16.

    But no matter what I try, it's always disposed: Context is always disposed Code calling the function:

    private void UIOMaticObjectService_UpdatedObject(object sender, UIOMatic.ObjectEventArgs e) { if (e.ObjectType.Name == "ActivitiesSchema") { var activitySchema = (ActivitiesSchema)e.Object;

                var result = nodeService.GetActivityNodePublished(activitySchema.Id);
    

    Function:

    public class NodeService : INodeService
    {
        private readonly IUmbracoContextFactory contextFactory;
        private readonly IContentService contentService;
        private readonly IScopeProvider scopeProvider;
    
        public NodeService(IUmbracoContextFactory contextFactory, IContentService contentService, IScopeProvider scopeProvider)
        {
            this.contextFactory = contextFactory;
            this.contentService = contentService;
            this.scopeProvider = scopeProvider;
        }
    
    public IPublishedContent GetActivityNodePublished(int activityId)
            {
                var nodeId = GetActivityNodeId(activityId);
                using (UmbracoContextReference context = contextFactory.EnsureUmbracoContext())
                {
                    using (scopeProvider.CreateScope())
                    {
                        var result = context.UmbracoContext.Content.GetById(nodeId);
                        return result;
                    }
                }
            }
    

    Can anybody help me out here?

  • Marc Goodson 1595 posts 10625 karma points MVP 6x c-trib
    23 days ago
    Marc Goodson
    0

    Hi Sibren

    Have you tried creating your Scope, before grabbing hold of your UmbracoContext...?

    The suggestion in the post:

    https://our.umbraco.com/forum/using-umbraco-and-getting-started/102676-triggering-index-rebuild-via-hangfire-causes-objectdisposedexception-in-nucache#comment-321129

    is to create a Scope if you are unsure of whether one exists or not...

    Inside this Scope using statement, you'll either have access to a Transient Scope, either 'nested' or a new one will be created. THEN you can can call EnsureUmbracoContext, to get reference to the UmbracoContext on that Scope/Thread... whereas in your example you are doing that 'before' creating the scope!

    eg

    public IPublishedContent GetActivityNodePublished(int activityId)
            {
                var nodeId = GetActivityNodeId(activityId);
          using (var scope = _scopeProvider.CreateScope(autoComplete: true))
                    {
                using (UmbracoContextReference context = contextFactory.EnsureUmbracoContext())
                    {          
                        var result = context.UmbracoContext.Content.GetById(nodeId);
                        return result;
                    }
                }
            }
    

    regards

    Marc

  • Sibren 36 posts 188 karma points
    22 days ago
    Sibren
    0

    Hi Marc,

    Thanks for your reply. I've been breaking my head over this, but I've found it: somewhere a URL was taken in code to get an URL via DI (other things where also injected into that class via DI). This caused errors all over the place in the entire project. Once it was changed back to

    Umbraco.Web.Composing.Current.UmbracoContext.HttpContext.Request.Url
    

    All issues magically disappeared.

Please Sign in or register to post replies

Write your reply to:

Draft