ash*_*lon 2 c# connection-string hangfire asp.net-core
我正在尝试将Hangfire与SQL Server配合使用,从appsettings.json文件中读取连接字符串。没用 仅当我将连接字符串提供给UseSqlServerStorage方法时,它才起作用。
这是appsettings.json:
{
"ConnectionStrings": {
"HangfireDemo": "Data Source=VSQL64;Initial Catalog=HangfireDemo;Integrated Security=SSPI;"
},
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是在Startup.ConfigureServices中配置Hangfire的行:
services.AddHangfire(configuration => configuration.UseSqlServerStorage("Data Source=VSQL64;Initial Catalog=HangfireDemo;Integrated Security=SSPI;"));
Run Code Online (Sandbox Code Playgroud)
如果我在UseSqlServerStorage方法中编写“ HangfireDemo” ,则无法使用。此方法中只有完整的连接字符串有效。
如何仅提供连接字符串名称?
您应该可以执行以下操作:
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
builder.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddHangfire(configuration =>
configuration.UseSqlServerStorage(Configuration.GetConnectionString("HangfireDemo"))
);
}
Run Code Online (Sandbox Code Playgroud)