我有一个.NET Core API项目,正在尝试从 appsettings.json 文件中读取一些设置。
文件如下:
{
"Logging": {
"IncludeScopes": false,
"Debug": {
"LogLevel": {
"Default": "Warning"
}
},
"Console": {
"LogLevel": {
"Default": "Warning"
}
}
},
"ConnectionString": "abc"
}
并且我正在尝试读取ConnectionString
设置,如下所示:
ConfigurationManager.AppSettings["ConnectionString"];
但是我得到null
,显然根本没有检测到任何应用程序设置。
编辑:
这是我现在拥有的:
startup.cs
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public static IConfiguration Configuration { get; set; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<MyContext>();
services.AddMvc();
services.Configure<ConnectionStringSettings>(Configuration);
}
}
ConnectionStringSettings.cs
public class ConnectionStringSettings
{
public string ConnectionString { get; set; }
}
appsettings.json
{
"Logging": {
...
},
"ConnectionStrings": {
"Debug": "abc"
}
}
MyContext.cs 公共类MyContext:DbContext { 字符串x;
public MyContext() { }
public MyContext(DbContextOptions<MyContext> options)
: base(options) { }
public MyContext(IOptions<ConnectionStringSettings> connectionStringSettings)
{
x = connectionStringSettings.Value.ConnectionString;
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
//here I need my connection string
optionsBuilder.UseMySql(connectionString);
}
}
答案 0 :(得分:3)
对于.NET Core 2.x,您需要在启动时进行如下配置
System.getTimestamp()
然后按以下方式访问
public partial class Startup
{
public static IConfiguration Configuration;
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
...
}
您还应该按如下所示布置连接字符串。
Configuration.GetConnectionString("System")
这将允许多个字符串(如果需要),即使您只有一个也可以。
编辑:一旦获得字符串,就象ColinM在他的回答中所说的那样,您需要将一个字符串注册到一个可以注入到您的类中的类中。正在启动,如下所示。
"ConnectionStrings": {
"System": "{connection String}"
}
您的连接字符串类...
services.AddSingleton<IConnectionStringFactory, ConnectionStringFactory>(serviceProvider => new ConnectionStringFactory(Configuration.GetConnectionString("System")));
以这种方式注入您的班级...
public class ConnectionStringFactory : IConnectionStringFactory
{
private readonly string _connectionString;
public ConnectionStringFactory(string connectionString)
{
_connectionString = connectionString;
}
public string Invoke()
{
return _connectionString;
}
}
您的界面可以如下所示
public class ClassName
{
private readonly IConnectionStringFactory _connectionStringFactory;
public ClassName(IConnectionStringFactory connectionStringFactory)
{
_connectionStringFactory = connectionStringFactory;
}
...
}
您不需要使用界面,但我建议您使用这种方法
答案 1 :(得分:2)
根据您在上下文中的OnConfiguring
覆盖中设置连接字符串的要求,可以使用以下方法。
更新您的 appsettings.json 配置文件以包含用于数据库选项的新JSON对象
{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"DatabaseOptions": {
"ConnectionString": "<Connection String>"
}
}
创建您的 DatabaseOptions 配置将映射到的模型类
public class DatabaseOptions
{
public string ConnectionString { get; set; }
}
然后更新您的Startup
类的ConfigureServices
方法以注册IOptions<DatabaseOptions>
的实例
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.Configure<DatabaseOptions>(Configuration.GetSection("DatabaseOptions"));
}
最后,更新您的上下文构造函数以注入您的IOptions<DatabaseOptions>
实例,只要您在服务集合中注册了上下文,ASP.NET Core就会为您处理依赖项注入。
public class MyContext
{
private readonly DatabaseOptions databaseOptions;
public MyContext(DbContextOptions<MyContext> options, IOptions<DatabaseOptions> databaseOptions)
: base(options)
{
this.databaseOptions = databaseOptions.Value;
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
//here I need my connection string
optionsBuilder.UseMySql(databaseOptions.ConnectionString);
}
}
我个人不喜欢将名称与 Options 命名的类将与IOptions<T>
一起使用,但是作为开发人员,我们可以花费大量时间进行编码只需考虑类和变量名,即可随意重命名。
答案 2 :(得分:0)
您应该在启动时使用配置=>
public void ConfigureServices(IServiceCollection services)
{
var section = Configuration.GetSection("ConnectionString");
var value= Configuration.GetValue<string>("ConnectionString")
}