Copied to clipboard

Flag this post as spam?

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


  • Allan Svelmøe Hansen 25 posts 136 karma points
    Nov 26, 2019 @ 12:01
    Allan Svelmøe Hansen
    0

    UmbracoContext issues in hangfire/service job. (possible Dependency Injection)

    Hi.

    I'm using the LightInject Dependency Injection and running Hangfire to do some scheduled tasks. I think I've gotten Hangfire to work using one of two methods:

    One by mocking a HttpContext when resolving the Scope of Lightinject, or two - by using another container for Hangfire and re-registering all services from the existing container.

    Both of these seems to work. My issue comes from the application layer which I'm calling from my Hangfire job.

    Suppose the following simplified example: From Hangfire I'm calling MyService.MyMethod() which needs to lookup content from Umbraco. I am doing the following simplified:

    public class MyService : IMyService
    {
        private readonly IUmbracoContextFactory _umbracoContextFactory;
    
        public MyService (IUmbracoContextFactory umbracoContextFactory)
        {
            _umbracoContextFactory = umbracoContextFactory;
        }
    
      public void MyMethod()
     {
           using (var contextFactory = _umbracoContextFactory.EnsureUmbracoContext())
            {                
                var myContent = contextFactory.UmbracoContext.Content.GetAtRoot().OfType<MyContent>()?.FirstOrDefault();
            }
     }
    }
    

    This does seem to work and I can view the correct content in myContent.

    However afterwards, from somewhere, I also get a null pointer exception with the stacktrace

    at Umbraco.Web.Templates.TemplateUtilities.ParseInternalLinks(String text, Boolean preview, UmbracoContext umbracoContext)
           at Umbraco.Web.PropertyEditors.ValueConverters.TextStringValueConverter.ConvertSourceToIntermediate(IPublishedElement owner, IPublishedPropertyType propertyType, Object source, Boolean preview)
           at Umbraco.Core.Models.PublishedContent.PublishedPropertyType.ConvertSourceToInter(IPublishedElement owner, Object source, Boolean preview)
           at Umbraco.Web.PublishedCache.NuCache.Property.GetInterValue(String culture, String segment)
           at Umbraco.Web.PublishedCache.NuCache.Property.GetValue(String culture, String segment)
           at Umbraco.Web.PublishedPropertyExtension.Value[T](IPublishedProperty property, String culture, String segment, Fallback fallback, T defaultValue)
           at Umbraco.Web.PublishedContentExtensions.Value[T](IPublishedContent content, String alias, String culture, String segment, Fallback fallback, T defaultValue)
    

    This time the UmbracoContext is null in ParseInternalLinks It seems because "something" else is calling NuCache (perhaps a rendering that's triggered in the background or something?), but I don't understand what that is and I cannot seem to trace it back.

    Does anybody have something that can help me shed some light on the issue and maybe point out where I am taking a wrong turn?

    Thanks in advance.

  • Nik 1593 posts 7151 karma points MVP 6x c-trib
    Nov 26, 2019 @ 16:54
    Nik
    0

    Hi Allan,

    How are you setting up the lightinject activator and registering a job?

    Nik

  • Allan Svelmøe Hansen 25 posts 136 karma points
    Nov 27, 2019 @ 06:47
    Allan Svelmøe Hansen
    0

    Hi. I've used the code base for Hangfire.Lightinject to set up activators. Then I've tried registering all from the Umbraco Composite into a new Service Container and set it for Hangfire:

      var sc = new ServiceContainer();
      //registering my + umbraco stuff into new container
     GlobalConfiguration.Configuration.UseLightInjectActivator(sc);
    

    and I've tried creating a new HttpContext when resolving the scope in Hangfire.Lightinject. Something I saw elsewhere on the forum:

    Something similar to this:

    public LightInjecterScope(ServiceContainer container)
            {
                _container = container;
                CreateContext();
                _scope = _container.BeginScope();
            }
    
            private static void CreateContext()
            {
                if (HttpContext.Current == null)
                {
                    HttpContext.Current = new HttpContext(new HttpRequest("PerWebRequestScopeManager", "http://localhost/PerWebRequestScopeManager", string.Empty),
                        new HttpResponse(new StringWriter()));
                }
            }
    

    Both of these methods seems to be working when I trigger my hangfire job because I get the objects and data I expect.

     RecurringJob.AddOrUpdate<IMyService>(x => x.MyMethod(), "*/10 * * * *");
    

    Except for the fact that I also get the Nullpointer exception mentioned in my first post but which does not seem to originate from my service (based on stacktrace).

  • Maurizio Camporelli 4 posts 74 karma points
    Mar 10, 2020 @ 08:21
    Maurizio Camporelli
    0

    Hi Allan,

    It looks like I'm having similar issues. I also mock the HttpContext (as explained here), but when I try to access properties of a page retrieved within my scheduled job I get null reference exceptions. Did you manage to get it working?

    Thanks, Maurizio

  • Allan Svelmøe Hansen 25 posts 136 karma points
    Mar 13, 2020 @ 09:30
    Allan Svelmøe Hansen
    0

    No - I didn't find a cause/workaround to that specific issue, so I moved on to another approach of calling webrequests from my background jobs instead.

Please Sign in or register to post replies

Write your reply to:

Draft