0

I'm testing Hangfire in Asp.net Core 3.1 app. I want to persist data to sqlite database. I added connection string into appsettings.json file

"ConnectionStrings": {
"Default": "Data Source=C:\\database\\hangfireDb.db; Version = 3; New = True; Compress = True;"},

Here is Startup class

 public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
        
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllersWithViews();


        services.AddHangfire(configuration => configuration
                     .UseSimpleAssemblyNameTypeSerializer()
                     .UseRecommendedSerializerSettings()
                     .UseSQLiteStorage(Configuration.GetConnectionString("Default")));

    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }
        app.UseHttpsRedirection();
        app.UseStaticFiles();

        app.UseRouting();

        app.UseAuthorization();

        app.UseHangfireServer();
        app.UseHangfireDashboard();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");
        });
    }
}

Connection to database cannot be established

SQLite.SQLiteException: 'Could not open database file: Data Source=C:\database\hangfireDb.db; Version = 3; New = True; Compress = True; (CannotOpen)'

I can open database if I use only a path to database

services.AddHangfire(configuration => configuration
                     .UseSimpleAssemblyNameTypeSerializer()
                     .UseRecommendedSerializerSettings()
                     .UseSQLiteStorage("C:\\database\\hangfireDb.db"));

Or

"ConnectionStrings": {
"Default": "C:\\database\\hangfireDb.db"},

This does not look like a proper connection string. I think I'm missing something. Connection string should include "Data Source" and any additional tag, but in this case only path to database works.

4
  • What does Configuration.GetConnectionString("Default") return (debug it and check, don't guess)? Commented Jul 15, 2020 at 23:40
  • It returns entire string ""Data Source=C:\\database\\hangfireDb.db; Version = 3; New = True; Compress = True;" Commented Jul 15, 2020 at 23:43
  • Perhaps parse the data source out and pass it in (since the use of the path is working for you)? Commented Jul 16, 2020 at 1:22
  • At this point I will only use a path. Thanks Commented Jul 16, 2020 at 2:01

2 Answers 2

3

This does not look like a proper connection string

Indeed.

You're using Hangire.Storage.SQLite (according to the call to UseSQLiteStorage), which uses a minimal SQLite implementation, sqlite-net-pcl, and uses the constructor that only accepts a path string, not a connection string.

So you can't pass parameters to SQLite using the connection string with this library, as far as I can see.

Sign up to request clarification or add additional context in comments.

1 Comment

Interesting. I did not know that. Unfortunately, this is the only currently working sqlite extension for hangfire. Thanks
0

Hangire.Storage.SQLite only accepts a path to the database, not a connection string. You should pass C:\database\hangfireDb.db and not Data Source=C:\database\hangfireDb.db ...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.