Copied to clipboard

Flag this post as spam?

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


  • David Armitage 485 posts 2042 karma points
    Apr 18, 2022 @ 10:33
    David Armitage
    0

    Umbraco 9 - contentHelper.GetById shows old data for background task

    Hi All,

    I have a really strange error. I have a scheduled tasks that runs every day. I use hangfire to handle this.

    As part of the scheduled task I am doing a simple contentHelper.GetById(Id) lookup.

    For some reason the data that comes back is an old version. I test cleaning the database and xml cache from within Umbraco and this has Zero effect. The only way I can get it to see the new published data is by stopping visual studio, rebuilding and run again. This then seems to work.

    I ran some more test....

    1. I start the data from fresh so everything is looking good in the debugger.

    2. I then manually trigger the sceduled task (all ok)

    3. I then run the exact same method getting the content from RajorScript (all ok)

    4. I then edit the content node I am looking up within Umbraco backend (save and publish). I click on and off the node to check the changes have updated ok (they have)

    5. I then manually trigger my scheduled task and it is seeing the old data from before.

    6. I then run the exact same method again from Razor and the data is ok.

    7. I run my sceduled task again and the data is still the old data. So the exact same method is bringing back different data if called from Razor or my Background Task.

    If I stop Visual Studion, rebuild, run and test again then the data called from both in the background task and the RazorScript are the same again.

    Very Odd.

    Does anyone know why this might be happening and more importantly does anyone know how I can force my background tasks to get the most recent data?

    Here is the method I am using to get the data.

    public JobAlertSubscriber GetJobAlertSubscriber(int Id)
            {
                try
                {
                    JobAlertSubscriber jobAlertSubscriber = null;
    
                    using (UmbracoContextReference umbracoContextReference = _umbracoContextFactory.EnsureUmbracoContext())
                    {
                        var contentHelper = umbracoContextReference.UmbracoContext.Content;
                        jobAlertSubscriber = (JobAlertSubscriber)contentHelper.GetById(Id);
                    }
    
                    return jobAlertSubscriber;
                }
                catch (Exception e)
                {
                    _logger.LogError(e, "GetJobAlertSubscriber: Exception: {0} | Message: {1} | Stack Trace: {2}", e.InnerException != null ? e.InnerException.ToString() : "", e.Message != null ? e.Message.ToString() : "", e.StackTrace);
                }
    
                return null;
            }
    
  • David Armitage 485 posts 2042 karma points
    Apr 18, 2022 @ 11:20
    David Armitage
    100

    Hi All,

    I managed to find a work around. I would still like to undertsand what is happening here through.

    My work around was to wrap all my logic inside an UmbracoApiController. Then my background task simply does a HttpClient Get Request.

    This will do nicely for now though.

    Eg.

    string siteUrl = _config.GetValue<string>("SiteSettings:SiteUrl");
    HttpClient client = new HttpClient();
    await client.GetAsync(siteUrl + "/umbraco/Api/HangfireApi/SendJobAlerts");
    
Please Sign in or register to post replies

Write your reply to:

Draft