Format of the initialization string does not conform to specification starting at index 0 on services.AddHangfire

2.7k views Asked by At

I am trying to integrate hangfire into my application with the help of the documentation on their site however, I am getting this error when trying to run the application

System.ArgumentException: 'Format of the initialization string does not conform to specification starting at index 0.'

on this line of code in my Startup.cs file:

 services.AddHangfire(x => x.UseSqlServerStorage("HangfireDb"));

I have added the Db using Entity Framework Code first and this is what the files look like:

public partial class HangfireDbContext : DbContext
{
    public HangfireDbContext(DbContextOptions<HangfireDbContext> options) : base(options) { }
}

Using a DBFactory that calls on the connection string stored in the appsettings file.

public class HangfireDbContextFactory : IDesignTimeDbContextFactory<HangfireDbContext>
{
    public HangfireDbContext CreateDbContext(string[] args)
    {
        var basePath = AppContext.BaseDirectory;
        var environmentName = Environment.GetEnvironmentVariable(DalConstants.HostingEnvironment);

        return Create(basePath, environmentName);
    }

    private HangfireDbContext Create(string basePath, string environmentName)
    {
        IConfigurationRoot config = new ConfigurationBuilder()
        .SetBasePath(basePath)
        .AddJsonFile(DalConstants.AppSettingsName)
        .Build();

        string connectionString = config.GetConnectionString(DalConstants.HangfireDb);

        if (string.IsNullOrWhiteSpace(connectionString))
        {
            throw new InvalidOperationException($"Could not find a connection string named '{DalConstants.HangfireDb}'.");
        }

        var optionsBuilder = new DbContextOptionsBuilder<HangfireDbContext>();
        optionsBuilder.UseSqlServer(connectionString);

        return new HangfireDbContext(optionsBuilder.Options);
    }
}

appsettings.json

"ConnectionStrings": {

"HangfireDb": "Server=serverName;User Id=userName;password=userPassword;Database=HangfireDb;MultipleActiveResultSets=true"
 },

I have run the migrations and the db is successfully created and theres no issue there. Is there something i'm missing here? Thanks.

0

There are 0 answers