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?
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.
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).
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?
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.
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:
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
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.
Hi Allan,
How are you setting up the lightinject activator and registering a job?
Nik
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:
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:
Both of these methods seems to be working when I trigger my hangfire job because I get the objects and data I expect.
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).
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
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.
is working on a reply...