Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hangfire job on Console/Web App solution?

I'm new to Hangfire and I'm trying to understand how this works.

So I have a MVC 5 application and a Console application in the same solution. The console application is a simple one that just updates some data on the database (originally planned to use Windows Task Scheduler).

Where exactly do I install Hangfire? In the Web app or the console? Or should I convert the console into a class on the Web app?

like image 530
Jack Avatar asked Nov 21 '25 12:11

Jack


1 Answers

If I understand it correctly, the console in your solution is acting like an "pseudo" HangFire, since like you said it does some database operations overtime and you plan to execute it using the Task Scheduler.

HangFire Overview

HangFire was design to do exactly what you want with your console app, but with a lot more of power and functionalities, so you avoid all the overhead of creating all that by yourself.

HangFire Instalation

HangFire is installed commonly alongside with ASP.NET Applications, but if you carefully read the docs, you will surprisingly find this:

Hangfire project consists of a couple of NuGet packages available on NuGet Gallery site. Here is the list of basic packages you should know about:

  • Hangfire – bootstrapper package that is intended to be installed only for ASP.NET applications that uses SQL Server as a job storage. It simply references to Hangfire.Core, Hangfire.SqlServer and Microsoft.Owin.Host.SystemWeb packages.

  • Hangfire.Core – basic package that contains all core components of Hangfire. It can be used in any project type, including ASP.NET application, Windows Service, Console, any OWIN-compatible web application, Azure Worker Role, etc.

As you can see, HangFire can be used in any type of project including console applications but you will need to manage and add all the libraries depending on what kind of job storage you will use. See more here:

Once HangFire is Installed you can configure it to use the dashboard, which is an interface where you can find all the information about your background jobs. In the company I work, we used HangFire several times with recurring jobs mostly to import users, synchronize information across applications and perform operations that would be costly to run during business hours, and the Dashboard proved to be very useful when we wanted to know if a certain job was running or not. It also uses CRON to schedule the operations.

A sample of we are using right now is:

Startup.cs

public partial class Startup
{
    public void Configuration(IAppBuilder app)
    {
        //Get the connection string of the HangFire database 
        GlobalConfiguration.Configuration.UseSqlServerStorage(connection);
        
        //Start HangFire Server and enable the Dashboard
        app.UseHangfireDashboard();
        app.UseHangfireServer();
        
        //Start HangFire Recurring Jobs
        HangfireServices.Instance.StartSendDetails();
        HangfireServices.Instance.StartDeleteDetails();
    }
}

HangfireServices.cs

public class HangfireServices
{
    //.. dependency injection and other definitions
    
    //ID of the Recurring JOBS
    public static string SEND_SERVICE = "Send";
    public static string DELETE_SERVICE = "Delete";

    public void StartSend()
    {
        RecurringJob.AddOrUpdate(SEND_SERVICE, () =>
            Business.Send(), //this is my class that does the actual process
            HangFireConfiguration.Instance.SendCron.Record); //this is a simple class that reads an configuration CRON file
    }

    public void StartDeleteDetails()
    {
        RecurringJob.AddOrUpdate(DELETE_SERVICE, () =>
            Business.SendDelete(), //this is my class that does the actual process
            HangFireConfiguration.Instance.DeleteCron.Record); //this is a simple class that reads an configuration CRON file
    }
}

HangFireConfiguration.cs

public sealed class HangFireConfiguration : ConfigurationSection
{
    private static HangFireConfiguration _instance;

    public static HangFireConfiguration Instance
    {
        get { return _instance ?? (_instance = (HangFireConfiguration)WebConfigurationManager.GetSection("hangfire")); }
    }

    [ConfigurationProperty("send_cron", IsRequired = true)]
    public CronElements SendCron
    {
        get { return (CronElements)base["send_cron"]; }
        set { base["send_cron"] = value; }
    }

    [ConfigurationProperty("delete_cron", IsRequired = true)]
    public CronElements DeleteCron
    {
        get { return (CronElements)base["delete_cron"]; }
        set { base["delete_cron"] = value; }
    }
}

hangfire.config

<hangfire>
  <send_cron record="0,15,30,45 * * * *"></send_cron>
  <delete_cron record="0,15,30,45 * * * *"></delete_cron>
</hangfire>

The CRON expression above will run at 0,15,30,45 minutes every hour every day.

Web.config

<configSections>
   
    <!-- Points to the HangFireConfiguration class -->
    <section name="hangfire" type="MyProject.Configuration.HangFireConfiguration" />
    
</configSections>

<!-- Points to the .config file -->
<hangfire configSource="Configs\hangfire.config" />

Conclusion

Given the scenario you described, I would probably install HangFire in your ASP.NET MVC application and remove the console application, simple because it is one project less to worry about. Even though you can install it on a console application I would rather not follow that path because if you hit a brick wall (and you'll hit, trust me), chances are you'll find help mostly for cases where it was installed in ASP.NET applications.

like image 58
jpgrassi Avatar answered Nov 24 '25 09:11

jpgrassi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!