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.