Use Hangfire with SQLite in ASP.Net 4.5.

I’ll install Hangfire with SQLite, which could be quick and easy solution for small projects, which do not need full blown DB server.

Versions of each framework and libraries are as follow.

APS.Net MVC 4.5.2
SQLite 1.0.103.0
Hangfire 1.6.7.0
Hangfire SQLite extension 1.1.1.0

Confusion selecting correct SQLite library.

When go to http://hangfire.io/extensions.html page, we can see the extension list as below.

13

I will use Hangfire SQLite version 1.1.1, but if you click project name, Hangfire SQLite, it goes to https://github.com/vip32/Hangfire.SQLite which is not the source code of Hangfire SQLite nuget 1.1.1. This source code in github is not working version.

Actually, https://github.com/wanlitao/HangfireExtension is the source code of nuget 1.1.1 and this is the working version. So, if you want to look into the source code, be careful about this.

Installation.

Just for a purpose of demonstration, install ASP.Net MVC 4.5.2 using default project template in VS2015 and install SQLite using Nuget.

1

This should install System.Data.SQLite, System.Data.SQLite.EF and System.Data.SQLite.Linq

2

install Hangfire using Nuget.

3

This will install Hangfire.Core and Hangfire.SqlServer

4

install Hangfire SQLite extention using Nuget.

5

This will install Hangfire.SQLite

6

Do configuration and add a recurring job in Startup.cs

12

When configuring Hangfire to work with SQLite, use UseSQLiteStorage() and use connection string name, SQLiteHangfire, which can be anything and will declare it later in Web.config

As SQLite cannot handle concurrent request, set WorkerCount = 1.

Just for testing, I add a recurring job to print current time in VS output window in every one minute.

public void Configuration(IAppBuilder app)
{
 ConfigureAuth(app);

 // Hangfire configuration
 var options = new SQLiteStorageOptions();
 GlobalConfiguration.Configuration.UseSQLiteStorage("SQLiteHangfire", options);
 var option = new BackgroundJobServerOptions { WorkerCount = 1};
 app.UseHangfireServer(option);
 app.UseHangfireDashboard();

 // Add scheduled jobs
 RecurringJob.AddOrUpdate(() => Run(), Cron.Minutely);
}

public void Run()
{
 Debug.WriteLine($"Run at {DateTime.Now}");
}

Add connection string as below so that SQLite can be accessed.

<connectionStrings>
 <add name="SQLiteHangfire" connectionString="Data Source=E:\OneDrive\SourceCodes\Study\ASPNetHangfireSQLite\ASPNetHangfireSQLite\App_Data\Hangfire.sqlite" providerName="System.Data.SQLite.EF6" />
 </connectionStrings>

All done, now let’s run and browse to “/hangfire” by typing as such.

7

Cool, hangfire launched and looks working. One recurring job has been registered.

8

Jobs are running every minute.

11

Looking at the folder SQL database to be exist, SQLite database automatically created without any extra work, which is really nice feature.

9

In VS output window, we can see timestamp is written every minute, however the timestamp was not exactly every minute. I’m not sure why there is a few seconds diffrence between recurring run.

10

Summary

In summary, we could set up ASP.Net MVC with Hangfire and SQLite, which was straightforward. Hangfire was easy to set up. Dashboard was convenient to monitor and manage jobs. Automatically created database schema was pretty impressive.

Running this a few days, I could see a few issues.

  1. Hangfire server become 2 servers and 0 server, rather than keep 1 server.  It looks not confident about Hangfire with SQLite, but recurring job worked as expected during those times.
  2. SQLite database size keep increasing, which is concern in terms of performance once the size reached at some point. I think we may need some regular maintenance to keep the size and maintain performance.

 

 




If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

2 thoughts on “Use Hangfire with SQLite in ASP.Net 4.5.”

  1. Thanks for sharing.

    The “timestamp was not exactly every minute” issue is due to 15s QueuePollInterval by default. You can set a minor value to increase precision:
    new SQLiteStorageOptions() { QueuePollInterval = TimeSpan.FromSeconds(1) });
    (The side effect is higher frequency of database query.)

    1. Thanks for the tip, darkthread.

      That’s good to know. I’ll definitely get a benefit from this option in case I need more precision.

      Regards
      Joe

Leave a Reply

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