A minimal example using Hangfire with Cultiv.Hangfire in Umbraco that:
-
Dynamically schedules tasks based on a notification
Startup.cs
:services.AddUmbraco(_env, _config) //... .AddNotificationHandler<ContentPublishingNotification, RegisterHangfire> //...
RegisterHangfire.cs
:RecurringJob.AddOrUpdate<ServiceThatUsesUmbracoContext>(x => x.DoSomethingWithUmbracoContext() , "* * * * *");
-
Does something with an UmbracoContext
ServiceThatUsesUmbracoContext.cs
:public void DoSomethingWithUmbracoContext() { using (var ctxRef = ctxFactory.EnsureUmbracoContext()) { var contentAtRoot = ctxRef.UmbracoContext.Content!.GetAtRoot(); var ids = string.Join(", ", contentAtRoot.Select(x => x.Id)); Console.WriteLine($"Content IDs at root: {ids}"); } }
- Clone, and run.
- In the installer choose a DB type supported by Hangfire (SQL or LocalDb)
- Let the installer run (starter kit will be installed)
- Stop & start the site.
(There's a bug in the HangfireComposer that I've partially fixed but need to spend more time looking at) - Publish some content
- Check it works - in the console, or set a breakpoint.