0

我正在尝试借助他们网站上的文档将hangfire集成到我的应用程序中,但是,在尝试运行应用程序时出现此错误

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

在我的 Startup.cs 文件中的这行代码上:

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

我首先使用实体​​框架代码添加了 Db,文件如下所示:

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

使用调用存储在 appsettings 文件中的连接字符串的 DBFactory。

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);
    }
}

应用设置.json

"ConnectionStrings": {

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

我已经运行了迁移并且成功创建了数据库并且那里没有问题。我在这里缺少什么吗?谢谢。

4

0 回答 0