Scheduling and monitoring background tasks is challenging work. Every big application needs to implement background tasks to accomplish background work, such as data processing, email reminders, SMS queues, and email queues. Windows Service is the most typical approach to meet the necessity.
Today, we are going to set up Hangfire and write some code to schedule an initial job in the ASP.NET Core project.
Hangfire is an open-source library that schedules and executes background jobs in .NET applications. You'll be able to create a simple background process inside the same application pool or thread without creating separate applications. Hangfire creates background jobs in persistence storage, like MS SQL Server, Redis, MongoDB, and others, that may prevent you from losing the job of recycling IIS pools or exception prevalence.
ASP.NET Core is now a common platform for MVC and Web API, with no separate project creation needed. Let's create a new ASP.NET Core MVC project. After the project is created, install Hangfire from NuGet. You can install Hangfire either from the Package Management Console or NuGet Package Manager.
Install via NuGet
PM> Install-Package HangFire
We are going to install Hangfire using NuGet Package Manager. Search the Hangfire stable version(current 1.6.7) and install it in the project.

Image 1. Nuget package manager
After successful installation, let's configure Hangfire.
Configuration
Open the Startup.cs class and locate a ConfigureServices method to register Hangfire as a service.
public void ConfigureServices(IServiceCollection services)
{
// Add Hangfire services.
services.AddHangfire(x => x.UseSqlServerStorage(Configuration.GetConnectionString("DefaultConnection")));
// Add framework services.
services.AddMvc();
}
Here, we are registering Hangfire with SQL Server. We must provide a connection string to locate the SQL Server database named HangFireDb. DefaultConnection is a connection string name added to the appsettings.json file.
"ConnectionStrings": {
"DefaultConnection": "Data Source=.\\SQLEXPRESS2014;Initial Catalog=HangFireDb;Integrated Security=True;"
}
Once the service is configured, navigate to the Configure method to add the codes below. Here, app.UseHangfireDashboard() will set up the dashboard. While http://<website-url>/hangfire, and app.UseHangfireServer() will register a new instance for BackgroundJobServer.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseHangfireDashboard();
app.UseHangfireServer();
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
app.UseStaticFiles();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
Now, let's run the application. Hangfire dashboard is available in the browser by hitting http://<website-url>/hangfire url.

Image 2. Hangfire Dashboard
Hangfire offers an integrated web monitoring UI, which is used to control any aspect of background job processing, as well as statistics, exceptions, and background job history.
When the Hangfire Server starts, it'll look for a configured database and check for the required database schema. If the schema doesn't exist, it creates a schema. The below image shows a list of schemas created when running the application.

Image 3. Hangfire database schema
How does it work?
Hangfire handles different types of background jobs, and all of them are invoked in a separate execution context.
With Hangfire, you can create the following.







Ayesha ButtPosted May 8, 2020, 2:44 AM
Can anyone tell how to create custom job using it
Ayesha ButtPosted May 8, 2020, 2:41 AM
How can i create jobschedule() that can execute later ??
Saktiprasad SwainPosted Jan 5, 2020, 10:07 AM
U didn't said to install hangfire.sql server and getconnection string showing error. How u define
Humayun Kabir MamunPosted Dec 18, 2016, 2:46 AM
Nice article about HangFire...
Debendra DashPosted Dec 17, 2016, 3:29 PM
Good one i will try this...
Rathrola Prem KumarPosted Dec 15, 2016, 8:42 PM
Usefull Info Bhavik, TQ :)