C# Configuration.GetSection始终返回null
每次调用C# Configuration.GetSection始终返回null,c#,asp.net-core,.net-core,C#,Asp.net Core,.net Core,每次调用Configuration.GetSection,返回对象的Value属性总是空的 我的启动构造函数 public Startup(IHostingEnvironment env) { var builder = new ConfigurationBuilder() .SetBasePath(env.ContentRootPath) .AddJsonFile("appsettings.json", optional: false, reloadOn
Configuration.GetSection
,返回对象的Value
属性总是空的
我的启动
构造函数
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddEnvironmentVariables();
this.Configuration = builder.Build();
}
我的ConfigureServices
方法
public void ConfigureServices(IServiceCollection services)
{
services.Configure<SqliteSettings>(opts => Configuration.GetSection("SqliteSettings").Bind(opts));
services.AddOptions();
services.AddMvc();
}
我用来定义SqliteSettings的类
public class SqliteSettings
{
public string DataSource { get; set; }
public bool? NewDatabase { get; set; }
public int? Version { get; set; }
public string Password { get; set; }
public long? CacheSize { get; set; }
// More properties
}
我在想JSON可能需要有相同数量的属性来匹配,或者可能与数据类型定义有关,但这些定义可能完全不相关。只需修改
ConfigureServices
方法,如下所示:
public void ConfigureServices(IServiceCollection services)
{
services.AddOptions();
services.Configure<SqliteSettings>(Configuration.GetSection("SqliteSettings"));
services.AddMvc();
}
public void配置服务(IServiceCollection服务)
{
services.AddOptions();
services.Configure(Configuration.GetSection(“SqliteSettings”);
services.AddMvc();
}
它应该可以工作。这在控制台应用程序中对我很有效:
var connectionString = config["ConnectionStrings:DefaultConnection"]
必须在所有
服务之后进行。配置调用。对于Web Api Core 2.1,我需要在项目根文件夹(与Startup.cs相同的文件夹)中使用Program.cs
。如果您有appsettings.Development.json
文件,请确保“ConnectionStrings:DefaultConnection”appsettings.Development.json
和appsettings.json
文件中的设置相同
右键单击appsettings.json
并转到属性
选择复制到输出目录=始终复制
根据报告:
当GetSection返回匹配的节时,不会填充值。A.
当节存在时,返回键和路径
如果要查看该部分的值,需要调用GetChildren()方法:Configuration.GetSection(“SqliteSettings”).GetChildren()代码>
或者您可以使用:
Configuration.GetSection(“SqliteSettings”).Get()
。JSON不需要有相同数量的属性来匹配。不匹配的可为null的属性将设置为null,不可为null的不匹配属性将设置为其默认值(例如,int将设置为0)。尝试将ConfigurationProvider添加到ConfigurationBuilder
应用程序配置由以下站点提供:
- 使用文件配置提供程序的appsettings.json
- appsettings.{Environment}.json使用文件配置提供程序李>
如果所有其他答案都不能解决问题,另一个原因可能是Options类的属性是私有的(或者没有可访问的setter)。我的问题是.bind()方法的对象实例没有初始化
services.Configure<SqliteSettings>(opts => Configuration.GetSection("SqliteSettings").Bind(opts));
绑定了本地设置变量:
Configuration.GetSection(AppSettingsSectionName).Bind(opts);
集装箱登记是否单独进行:
services.Configure<AppSettings>(Configuration.GetSection("SqlliteSettings"));
services.Configure(Configuration.GetSection(“SqlliteSettings”);
这解决了问题,因为当提供的实例为null时,.Bind()方法将只返回null。如果依赖项注入失败,并且您在调试后得出结论认为此方法是问题所在,则不是
这就足够注册了:
services.Configure<SqliteSettings>(Configuration.GetSection("SqliteSettings"));
services.Configure(Configuration.GetSection(“SqliteSettings”);
您必须在接口中注入配置类。这将解决DI问题。我已经在这个问题上浪费了几个小时,希望它能帮助其他人。不幸的是,它不起作用,尽管下单可能会解决将来可能出现的错误Configuration.GetSection
仍然返回.Value
为空。您可以分享一下调用.Value
的方式吗?要查看我是否得到了JSON,只需执行var x=Configuration.GetSection(“SqliteSettings”)
然后在Visual Studio中检查x,特别是针对值。如果要直接从配置中读取,请使用类似配置[“SqliteSettings:DataSource”]的内容代码>。如果您想在控制器使用选项模式中使用它,我知道在Startup.cs中,您只能获得配置json的“叶子”,这与.net core 1中的不同,并使用这样的代码:services_u.Configure(options_u=>{options_u.Name=configuration.GetValue(“Corporate:Name”);options_u.BrandLogo=Configuration.GetValue(“Corporate:BrandLogo”);};Configuration.GetSection(“SqliteSettings”).Bind(sqliteSettingsObject);是读取设置的另一种方法如果只需要查看节是否存在,可以使用Exists()方法,如Configuration.GetSection(“SqliteSettings”).Exists()
the.Get()版本适合我。令人沮丧的是,MSDN只是显示了正在使用的铸态,我打赌这会让很多人感到困惑。非常感谢。显然,internal
不足以用于此目的。谢谢:文件appsettings.Development.json
出现在我的源文件夹中,但没有显示在VS2019中。这么简单的事情浪费了这么多时间。。。。。谢谢
Configuration.GetSection(AppSettingsSectionName).Bind(opts);
services.Configure<AppSettings>(Configuration.GetSection("SqlliteSettings"));
services.Configure<SqliteSettings>(Configuration.GetSection("SqliteSettings"));