UmbracoContext in Hangfire job (using the Cultiv p...
# help-with-umbraco
s
Hey! Trying to access content in Umbraco from a hangfire job using the cultiv package, but for some reason it's not quite working. Here's the simple setup, and the job itself triggers without issue: HangfireComposer.cs (see image) SynchronizationJob.cs (see image) Alternative 1 Now to the issue at hand. Alternative 1 works but when attempting to fetch the property, it returns null instead of values that I know exist, companyContainerMappingsInternal is a simple textstring on the startPage-node, and if I check the "Properties", it sure enough has the values there. Alternative 2 GetAtRoot returns null, but faking it by throwing the root1.Id into getbyid seems to work even though it's not a valid solution, but all defined properties throw something like this
'((Umbraco.Cms.Web.Common.PublishedModels.StartPage)root2).CompanyContainerMappingsInternal' threw an exception of type 'System.ArgumentNullException'
. So for some reason I cannot access Umbraco propertly in the jobs. I've tried looking at @Sebastiaan's [blogpost](https://cultiv.nl/blog/using-hangfire-to-update-umbraco-content/) about it but it did not seem to work either. https://cdn.discordapp.com/attachments/1285548823573168201/1285548824454103071/image.png?ex=66eaac24&is=66e95aa4&hm=1695d295ae360e54e076b832aae765f65827771880a5afe7959926b524315af7&
d
have you seen this blog post : https://cultiv.nl/blog/using-hangfire-to-update-umbraco-content/ The content cache is accessed differently there
s
Yes I tried using that approach as well , but it made no difference
For reference, here is the blogpost solution implemented, but still returning null even though I have an entire site setup.
j
Try
Copy code
csharp
var rootNode = _.UmbracoContext.Content?.GetAtRoot().FirstOrDefault();
Atleast we have several hangfire jobs where we access the content cache and can get properties using an approach like this:
Copy code
csharp
using var context = _umbracoContextFactory.EnsureUmbracoContext();

var archive = context
    .UmbracoContext.Content?.GetAtRoot()
    .FirstOrDefault();
s
I'll give it a try 🙂
s
do you have very by culture set? If so, the variationcontextaccessor needs to be set to the right culture
s
hmm I see, that might be something. let me try.
s
Copy code
public string Example(string culture)
 {
     _variationContextAccessor.VariationContext = new VariationContext(culture);

     var context = _umbracoContextFactory.EnsureUmbracoContext();
     var home = context.UmbracoContext.Content?.GetAtRoot().FirstOrDefault();
     var homeId = home?.Id ?? -1;

     return $"Task ran with culture: {culture}. Found Home node id: {homeId}. Ran on machine: {Environment.MachineName}";
 }
is an example task we made
s
Sweet, thanks, I'll give that a try 🙂
Yes, that worked perfectly, thank you 🙂
s
awesome
11 Views