1. Introduction
  2. Creating the basic form
  3. Integrating with AWS
  4. Integrating with S3
  5. Adding background tasks
  6. Sending emails and creating download links


Before moving to the last part of the series – adding emails and download links -, let’s look at a feature that will be pretty helpful for both what we’ve built so far, as well as what we are going to build.

Background tasks provide a way for us to run certain processes without having the user wait for them. Take a task like sending emails or deleting files from S3: we can let the user know it will be done and that let them continue their day; there is no purpose in the user waiting for the files to upload from our server to S3, as there is nothing they can do about it.

While ASP.NET Core provides background tasks as a built-in feature, it lacks something that Hangfire (the library we are going to use) has: persistence. Hangfire will save the tasks that we schedule in the database, and even in situations where our servers restart or go down for some reason, it assures us that the tasks will resume.

Setting up Hangfire

The first step is to install the NuGet package, by right clicking on your project in the Solution Explorer and selecting “Manage NuGet Packages…”. Search for “Hangfire” and install the latest package:

Then, open the Startup class and add the two Hangfire lines to your ConfigureServices method; since we are using the connection string in two places, I created a variable to hold it:

Also, add the first line to your Configure method; it’s important you have it before any routing happens, as otherwise ASP.NET Core will try to map it to a Blazor page:

You can add it inside the if that checks if the application environment is set to development if you want to not have the Hangfire dashboard in production – which is a good idea, as people would be able to mess with your background tasks otherwise.

Scheduling tasks

After adding the Hangfire dependencies into the Startup class, we can inject instances of IBackgroundJobClient to enqueue and schedule background tasks. For now, we will only use background tasks in the FileHandler class, so le’ts inject it in there:

We will schedule a task for deleting files from S3 in both the DownloadFileAsnyc and UploadFileAsync methods. For the first one, we will tell Hangfire to call the method 30 minutes from the current time – this is in case something goes wrong with our server or the user is otherwise unable to download the file on the first try:

On the other hand, in the second method, we will schedule the task for 24 hours from the current time; the receiver has one day to download the file before it is deleted completely:

Testing the tasks

You can run your application and go to the ‘/hangfire’ path to see the Hangfire dashboard. Watch the following video to see how to view your scheduled tasks:

No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *

Learn ASP.NET Core!
Get the latest articles on .NET and cloud!