Copied to clipboard

Flag this post as spam?

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


  • Yannic 2 posts 82 karma points
    Aug 18, 2022 @ 12:13
    Yannic
    0

    EnsureUmbracoContext gives no content

    Hi all,

    I'm trying to add Hangfire to my application. Every night, some tasks need to be performed. For that, I need access to the umbraco content. As tasks in Hangfire do not have an HttpContext, I use the UmbracoContextFactory:

    public class TestService : BaseService
    {
        private readonly IUmbracoContextFactory _umbracoContextFactory;
    
        public TestService(
            CustomDbContext db,
            IUmbracoContextFactory umbracoContextFactory
            ) : base(db)
        {
            _umbracoContextFactory = umbracoContextFactory;
        }
    
        public string Test()
        {
            using var ucr = _umbracoContextFactory.EnsureUmbracoContext();
            var root = ucr.UmbracoContext.Content.GetAtRoot().FirstOrDefault();
    
            return "";
        }
    }
    

    However, root is always null if this is called by Hangfire. If I call this from a controller, I do get the root.

    What is the correct/best way to get the root content node?

    Cheers, Yannic

  • Huw Reddick 1932 posts 6722 karma points MVP 3x c-trib
    Aug 19, 2022 @ 07:26
    Huw Reddick
    0

    I just use IContentService in my hangfire job

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies