Skip to the content

Scheduled or Recurring Tasks in Umbraco 9

Creating scheduled tasks or reoccurring tasks in Umbraco 9 is straight forward. In fact, I do not think there is anything special about the way Umbraco does this. It appears they just use Microsoft’s out of the box “RecurringHostedService”.

To demonstrate how to do this it is probably better to give you a code sample. Below is some example code on how to first create a very basic scheduled task and the second is how to inject this into your Umbraco 9 web application.

This is how to create a scheduled task.

using Microsoft.Extensions.Logging;
using System;
using System.Threading.Tasks;
using Umbraco.Cms.Infrastructure.HostedServices;
namespace Website.Core.Events
{
public class ScheduledTasks : RecurringHostedServiceBase
{
private readonly ILogger<ScheduledTasks> _logger;
public ScheduledTasks(ILogger<ScheduledTasks> logger) : base(TimeSpan.FromMinutes(1), TimeSpan.FromMinutes(2))
{
_logger = logger;
}
public override async Task PerformExecuteAsync(object state)
{
try
{
string test = "test";
}
catch (Exception e)
{
_logger.LogError("exception when persisting collections: {exception}", e.ToString());
}
}
}
}

Here is how to inject your scheduled task into your application inside the startup.cs file

public void ConfigureServices(IServiceCollection services)
{
#pragma warning disable IDE0022 // Use expression body for methods
services.AddUmbraco(_env, _config)
.AddBackOffice()
.AddWebsite()
.AddComposers()
.AddServices()
.AddEvents()
.Build();
services.AddHostedService<ScheduledTasks>();
#pragma warning restore IDE0022 // Use expression body for methods
}

If you are looking for other ways to inject the scheduled tasks into your web application you might find this article useful 'Different Ways to Inject Events, Helpers and Services into Umbraco 9'.

Keep an eye out for my follow up articles.

1. 'How to install Hangfire in Umbraco 9'.
2. 'Creating my First Package in Umbraco 9 (Hangfire Dashboard)'

About the author

David Armitage

.Net MVC Developer
.Net Core Developer
Umbraco Certified Master
Recruitment Professional

Hey Peeps,

I'm an entrepreneur and web developer with a passion for coding. I absolutely love working with Umbraco CMS and appreciate the Umbraco community even more.

I've got 10 years+ .Net experience and 7 years+ experience working in the recruitment industry, both coding and marketing job websites. I wanted to use my skills to help give something back to this awesome community and so I decided to build UmbraJobs.com.

My vision & fundamentals

I want this website to be a place where Umbraco Professionals and Umbraco Partners can advertise their services but at the same time I want to filter out all the under-qualified freelancers & agencies that take up the biggest part of other job & freelancer websites. We've all worked hard to gain our Umbraco certifications, partnership status, and help build the Umbraco community to what it is today. I want to see everyone get rewarded for their efforts.

Follow me on social media

If you're an Umbraco professional, partner, employer, recruiter or a passionate Umbraco community builder then I'm more than happy to hear from you. Follow me on my social media channels and please reach out if you have any needs, want help or consultation with anything Umbraco related or just want a general chat.

comments powered by Disqus

Blog Filter


How we can help?

Need help with an Umbraco project?

Need help with a project but not too sure who to approach? Send us your project brief and budget. We will provide a free consultation and can help you gather quotes from the best and most suitable freelancers or agencies.

Looking to hire an Umbraco Professional?

Have you got job vacancy and want to hire an Umbraco professional? Post a job on our website for free. Alternatively let us know your requirements and we will send suitable prospects your way.

Claim your free profile!

Are you an Umbraco Freelance Developer or Umbraco partner that wants to advertise on our site? If you work with Umbraco or registered as an Umbraco partner then you can create a profile for free.

Let's build the Umbraco Community

We're big on building the Umbraco community and we think you guys are awesome! If there's anyway at all we can help then please reach out.