Tag

sqlite

Browsing

Configuring ASP.NET WebForms, Hangfire and Sqlite storage will work with the following setup:

First, install these packages

<package id="Hangfire.AspNet" version="0.2.0" targetFramework="net472" />
<package id="Hangfire.Core" version="1.7.19" targetFramework="net472" />
<package id="Hangfire.Dashboard.Authorization" version="3.0.0" targetFramework="net472" />
<package id="Hangfire.Storage.SQLite" version="0.2.5" targetFramework="net472" />

The last one is the non-abandoned one with the description of “An Alternative SQLite Storage for Hangfire”
When you install this Hangfire.Storage.SQLite package, it will auto-install these:

<package id="sqlite-net-pcl" version="1.6.292" targetFramework="net472" />
<package id="SQLitePCLRaw.bundle_green" version="1.1.13" targetFramework="net472" />
<package id="SQLitePCLRaw.core" version="1.1.13" targetFramework="net472" />
<package id="SQLitePCLRaw.lib.e_sqlite3.linux" version="1.1.13" targetFramework="net472" />
<package id="SQLitePCLRaw.lib.e_sqlite3.osx" version="1.1.13" targetFramework="net472" />
<package id="SQLitePCLRaw.lib.e_sqlite3.v110_xp" version="1.1.13" targetFramework="net472" />
<package id="SQLitePCLRaw.provider.e_sqlite3.net45" version="1.1.13" targetFramework="net472" />
<package id="Stub.System.Data.SQLite.Core.NetFramework" version="1.0.113.3" targetFramework="net472" />

I guess some of them is not required, but hell, I’ve spent to much time on getting all this to work…

Now, use something like SQLiteStudio (https://sqlitestudio.pl) and create an empty database called Hangfire.db. In my code example, I called it “Jobs.db”)

Now change your startup.cs to look something like this:

using Microsoft.Owin;
using Owin;
using Hangfire;
using System;
using Hangfire.Dashboard;
using System.Web;
using Hangfire.Storage.SQLite;

[assembly: OwinStartupAttribute(typeof(WLMT.Startup))]

namespace WLMT
{
    public partial class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            ConfigureAuth(app);
            string hangfireSqliteDb = String.Format(@"{0}App_Data/Jobs.db", AppDomain.CurrentDomain.BaseDirectory);
            var storeageOptions = new SQLiteStorageOptions();
            storeageOptions.QueuePollInterval = TimeSpan.FromSeconds(30);
            GlobalConfiguration.Configuration.UseSQLiteStorage(hangfireSqliteDb, storeageOptions);
            var backgrounJobServerOption = new BackgroundJobServerOptions { WorkerCount = 1 };
            var dashboardOptions = new DashboardOptions
            {
                Authorization = new[] { new JobDashboardAuthorization() },
                AppPath = VirtualPathUtility.ToAbsolute("~"),
                DashboardTitle = "WLMT",
                DisplayStorageConnectionString = false
            };
            app.UseHangfireDashboard("/jobs", dashboardOptions);
            app.UseHangfireServer(backgrounJobServerOption);
        }
    }
}

You almost there. You will see that I have my own Authorization. This is because the default method did not work in Webforms. Its as if some sequence of something is wrong and I kept getting now page displaying due to Authentication failure. To fix this, create your own IDashboardAuthorizationFilter. Create a new class in your Code / App_Code folder, or wherever…. here is how my class looks like:

using Hangfire.Annotations;
using Hangfire.Dashboard;
using System.Web;

namespace WLMT
{
    public class JobDashboardAuthorization : IDashboardAuthorizationFilter
    {
        public bool Authorize([NotNull] DashboardContext context)
        {
            bool hasAccess;
            try
            {
                hasAccess = HttpContext.Current.User.IsInRole("Admin");
            }
            catch
            {
                hasAccess = false;
            }
            return hasAccess;
        }
    }
}

That’s it!

In my example, once logged in with a user that belongs to the “Admin” role, I can navigate to /jobs (see my path app.UseHangfireDashboard(“/jobs”, dashboardOptions);) and Hangfire GUI display and everything seems to work.

Take note of this line: GlobalConfiguration.Configuration.UseSQLiteStorage(hangfireSqliteDb, storeageOptions);

UseSQLiteStorage(hangfireSqliteDb, storeageOptions); hangfireSqliteDb is the connection string.
Do not try things like File=… or Data Source=… like you do in other connection string. This is NOT A CONNECTION STRING. Its is FILEPATH.

You need to provide a path like “c:\yourdirectory\your.db”.
Because this is serverless and runs within my web application I just defined my path like this:

string hangfireSqliteDb = String.Format(@"{0}App_Data/Jobs.db", AppDomain.CurrentDomain.BaseDirectory);