Copied to clipboard

Flag this post as spam?

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


  • Mateusz Kotwica 29 posts 129 karma points
    Apr 25, 2024 @ 09:19
    Mateusz Kotwica
    0

    Umbraco + Hangfire - injecting multiple services

    Hello,

    I have a similar issue to this one: https://our.umbraco.com/forum/using-umbraco-and-getting-started/110698-access-content-cache-from-hangfire-in-v10-11-whats-going-on

    Long story short: I'm trying to inject a UmbracoContextFactory and one of my custom services into my Hangfire task. All works ok if I follow the advise from the thread above, which is: I inject just UmbracoContextFactory. However when I inject also my custom service I start to receive following error: enter image description here

    Has anybody faced with such a issue before and can help?

  • Bo Jacobsen 601 posts 2399 karma points
    Apr 25, 2024 @ 09:27
    Bo Jacobsen
    0

    Hi Mateusz.

    Does your own custom service use UmbracoContext that it not called by UmbracoContextFactory by any chance?

    You need to be sure that all UmbracoContext is called by the Factory like.

    using (var ctx = UmbracoContextFactory.EnsureUmbracoContext())
    {
        // ctx.UmbracoContext.Content.GetById(1000);
    }
    
  • Mateusz Kotwica 29 posts 129 karma points
    Apr 25, 2024 @ 09:34
    Mateusz Kotwica
    0

    Hi Bo,

    indeed, my custom service uses UmbracoContextFactory by injection but when it comes to use it, in every single place following pattern is applied:

    using (var cref = _umbracoContext.EnsureUmbracoContext())
    {
        // do stuff...
    }
    

    Maybe I need to clarify, the mentioned error appears during Task instance initialization, which means the execution does not even reach the construstor.

  • Bo Jacobsen 601 posts 2399 karma points
    Apr 25, 2024 @ 09:42
    Bo Jacobsen
    0

    It looks like its the IUmbracoContextAccessor that is coursein the error, but i am not sure.

    How does your Class looks like?

  • Mateusz Kotwica 29 posts 129 karma points
    Apr 25, 2024 @ 10:06
    Mateusz Kotwica
    0

    Ok so here is a task class implementation:

    public interface IDummyTask
    {
        void Execute(PerformContext context, CancellationToken token);
        void DoJob();
    }
    
    public class DummyTask : IDummyTask
    {
        private readonly string _taskName;
        private readonly IUmbracoContextFactory _umbracoContextFactory;
        private readonly IImportTourDataService _tourImporter;
    
        public DummyTask(IDummyService dummyService, IImportTourDataService importer, IUmbracoContextFactory umbracoContextFactory)
        {
            _taskName = dummyService.GetTaskName();
            _umbracoContextFactory = umbracoContextFactory;
            _tourImporter = importer;
        }
    
        public void DoJob()
        {
            Thread.Sleep(5000);
        }
    
        public void Execute(PerformContext context, CancellationToken token)
        {
            RecurringJob.AddOrUpdate<IDummyTask>(_taskName, x => x.DoJob(), "* * * * *");
        }
    }
    
    public interface IDummyService
    {
        string GetTaskName();
    }
    
    public class DummyService : IDummyService
    {
        public string TaskName => "DummyTask";
    
        public string GetTaskName()
        {
            return TaskName;
        }
    }
    

    When I remove IImportTourDataService from the constructor - all works fine. So it seems to be an issue.

    So here is the constructor's header from the IImportTourDataService implementation:

    public ImportTourDataService(IUmbracoContextFactory umbracoContext,
                                     IExamineManager examineManager, 
                                     IHubContext<TourImportHub> hubContext, 
                                     IContentService contentService,
                                     ITourImporter<ExtendedSeasonNodeImporter> tourImporter,
                                     ITourImporter<DateRateCoordinatedDbImporter> datesAndRatesImporter,
                                     ISiteManager siteManager, 
                                     ILogger logger, 
                                     IBrandRegionsService brandRegionsService,
                                     IScopeProvider scopeProvider,
                                     IFacebookFeedService facebookFeedService, 
                                     IIndexCreatorService indexCreator)
    
  • Mateusz Kotwica 29 posts 129 karma points
    Apr 25, 2024 @ 10:15
    Mateusz Kotwica
    0

    Forgot to mention: both my services: DummyTask and ImportTourDataService are registered in IoC as transient.

  • Mateusz Kotwica 29 posts 129 karma points
    Apr 25, 2024 @ 10:32
    Mateusz Kotwica
    0

    Another observation: when I inject IDummyTask (with all 3 services injected via DummyTask constructor) into some controller for example it also seems to work.

    It looks like the issue with how Hangfire resolves dependencies (?)

Please Sign in or register to post replies

Write your reply to:

Draft