Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
I'm migrating a project from 7.X to 8 in order to familiarize myself with this brand new Umbraco version!
Happens that this project relies on background jobs (using Hangfire) for some recurring tasks, and those tasks need to access IPublishedContent.
In the previous version faking, an UmbracoContext was somewhat easy with a EnsureUmbracoContext method that would create a context using UmbracoContext.EnsureContext if UmbracoContext.Current was null and it worked pretty well.
With Umbraco8 and all the changes under the hood I have yet to figure out a way to access/create a UmbracoContext and/or IUmbracoContextAccessor for those cases or event setup a LightInject container within my Hangfire server to with all the dependencies resolved.
I have been fiddling in Umbraco source code in order to try to figure a way but without success so far.
Can anybody point me in the right direction?
P.S.: I know that an easy way to solve this would be to have an API endpoint and make my background job call that endpoint, solving my issue. But I'm not interested in such inelegant solution.
For the quick-and-dirty solution:
using Umbraco.Web.Composing; and use Current.UmbracoContext.what_you_need.
Don't forget to Install-Package UmbracoCms.Web, which is a new NuGet package that we didn't have for v7.
Thanks for the quick reply! H5YR!
I will definitively try that.
But, will the UmbracoContext be setup in this case? Since there is no HttpRequest associated with my Job?
Well, going to try that out and will be back with an update.
Yeah.. that is a really good question and I'm not sure of that either. Give it a go and see how far you get.
For future improvements I'm curious as to what exactly you're trying to access that need a request context, might be able to figure it out with you!
Might be of interest, just blogged about using Hangfire with Umbraco 8: https://cultiv.nl/blog/using-hangfire-for-scheduled-tasks-in-umbraco/
Yes thank you, somebody actually already linked that blog post in our slack. And its exactly what I needed! Marking your answer as the accepted one.
So I've been playing with this on my spare time and while I now have hangfire working based on the blog you shared I'm still blocked.
When in jobs (or registering the jobs) Umbraco.Web.Composing.Current has some of the key elements to null (UmbracoContext, UmbracoHelper, ...) and this is kind of expected, since there is no HttpRequest tied to them.
But Umbraco.Core.Composing.Current has all the values set, and the Factory returns a lot of instances. But the one I really want is not there.
I really would like my background job to access IPublishContent, for example, I have a settings DocType/Node with some settings that I need to access so I really would like a way to load that Node from NuCache so I know that I have what is actually published (and not IContent that might have changes that I don't want, since they are not published).
I probably could use IContent and the versions to find the latest one that is/was published (?) ... but is this the better/only way to do it?
I believe Umbraco.Core.Composing.Current should have a way to access the an instace of IPublishedCache.
Quick answer: there is a non-hackish way to access the front-end cache from within a background task - going to document it soon as I can.
Thank you Stephan!
I'll be waiting for your answer!
More details: the UmbracoContext is internally managed by an IUmbracoContextFactory - in most cases, you should not bother about it, but in your case, it's going to help.
There is, unfortunately, no Current.UmbracoContextFactory at the moment, though we may add it. So you will have to get the factory via:
var umbracoContextFactory = Current.Factory.GetInstance<IUmbracoContextFactory>();
(that is, assuming you cannot inject it - of course, ideally, you should inject it)
That factory can give you a reference to an UmbracoContext:
using (var contextReference = umbracoContextFactory.EnsureUmbracoContext())
var umbracoContext = contextReference.UmbracoContext;
What happens is: if there already is a "current" UmbracoContext, the reference will point to it. Otherwise, a new one will be created, and is registered as the curent one - until the reference is disposed, and then the context is disposed and is not current anymore.
Therefore, this is safe to use both within normal requests, and within background tasks.
You can access the context via the contextReference.UmbracoContext property but, since it's also the current one, Current.UmbracoContext will return it, and anything that requires a context will work.
Then, you can directly access umbracoContext.ContentCache to retrieve whatever you need.
Yes! This should do the trick!
I'm going to try it later this week and will give you feedback then!
Thanks a lot for your help!
Thank you so much!
is working on a reply...
Write your reply to:
Image will be uploaded when post is submitted