Configuration 在ASP.NET Core中的ConfigureServices阶段实际读取应用程序设置
我需要在ASP.NET Core 1.0 web应用程序的Configuration 在ASP.NET Core中的ConfigureServices阶段实际读取应用程序设置,configuration,asp.net-core,app-config,startup,sections,Configuration,Asp.net Core,App Config,Startup,Sections,我需要在ASP.NET Core 1.0 web应用程序的ConfigureServices方法中设置一些依赖项(服务) 问题是,基于新的JSON配置,我需要设置一个或另一个服务 我似乎无法实际读取应用生命周期的ConfigureServices阶段中的设置: public void配置服务(IServiceCollection服务) { var section=Configuration.GetSection(“MySettings”);//这实际上并不包含设置 services.Config
ConfigureServices
方法中设置一些依赖项(服务)
问题是,基于新的JSON配置,我需要设置一个或另一个服务
我似乎无法实际读取应用生命周期的ConfigureServices
阶段中的设置:
public void配置服务(IServiceCollection服务)
{
var section=Configuration.GetSection(“MySettings”);//这实际上并不包含设置
services.Configure(section);//这是一条设置指令,我无法使用这些设置获取MySettingClass实例
// ...
//设置服务
AddSingleton(typeof(ISomething),typeof(ConcreteSomething));
}
我需要实际阅读该部分并决定注册什么
ISomething
(可能与ConcreteSomething
)不同。从ASP.NET Core 2.0开始,在构建WebHost
实例时,我们在程序
类中进行配置设置。此类设置的示例:
返回新的WebHostBuilder()
.UseKestrel()
.UseContentRoot(目录.GetCurrentDirectory())
.ConfigureAppConfiguration((builderContext,config)=>
{
IHostingEnvironment env=builderContext.HostingEnvironment;
config.AddJsonFile(“appsettings.json”,可选:false,reloadOnChange:true)
.AddJsonFile($“appsettings.{env.EnvironmentName}.json”,可选:true,重载更改:true);
})
除此之外,这允许直接在Startup
类中使用配置,通过构造函数注入获得IConfiguration
的实例(谢谢,内置DI容器):
公共类启动
{
公共启动(IConfiguration配置)
{
配置=配置;
}
公共IConfiguration配置{get;}
...
}
这是您可以从appSettings.json
的ConfigureServices
方法中获取键入设置的方法:
公共类启动
{
公共启动(IConfiguration配置)
{
配置=配置;
}
公共IConfiguration配置{get;}
public void配置服务(IServiceCollection服务)
{
Configure(Configuration.GetSection(nameof(MySettings));
services.AddSingleton(配置);
// ...
var settings=Configuration.GetSection(nameof(MySettings)).Get();
int maxNumberOfSomething=settings.maxNumberOfSomething;
// ...
}
// ...
}
您可以通过配置[“ConfigSection:ConfigValue”])访问appsettings.json值。
appsettings.json文件:
"MyApp": {
"Jwt": {
"JwtSecretConfigKey": "jwtSecretConfigKey",
"Issuer": "appIssuer",
"Audience": "appAudience",
"ExpireMinutes": 60
}
}
添加一个类来绑定“MyApp”的Jwt部分
在配置服务方法中:
//reading config from appsettings.json
var jwtConfig = Configuration.GetSection("MyApp:Jwt").Get<JwtConfig>();
//using config data ....
services.AddJwt(new AddJwtOptions(jwtConfig.JwtSecretConfigKey, jwtConfig.Issuer, jwtConfig.Audience, jwtConfig.ExpireMinutes));
//从appsettings.json读取配置
var jwtConfig=Configuration.GetSection(“MyApp:Jwt”).Get();
//正在使用配置数据。。。。
services.AddJwt(新的AddJwtOptions(jwtConfig.JwtSecretConfigKey,jwtConfig.Issuer,jwtConfig.academy,jwtConfig.ExpireMinutes));
下面的代码与上面的代码相同。它也很有效
var jwtConfig = Configuration.GetSection("MyApp").GetSection("Jwt").Get<JwtConfig>();
var jwtConfig=Configuration.GetSection(“MyApp”).GetSection(“Jwt”).Get();
希望这有帮助。请看@ademcaglin:谢谢!就这样。我投票赞成以重复的方式结束我自己的问题:)链接的答案是从配置文件中获取值,而不是从appsettings.json文件中获取值。可能是重复的耶!为我工作。最后,我可以从json条目中获取我的资料来准备我的服务:\n当ASP.NET Core 2.1应用程序托管在IIS下时,这是如何工作的?不会使用
WebHostBuilder
,因此传递到Startup
的构造函数中的IConfiguration配置中有什么内容?不,对象配置为null
public class JwtConfig
{
public string JwtSecretConfigKey { get; set; }
public string Issuer { get; set; }
public string Audience { get; set; }
public int ExpireMinutes { get; set; }
}
//reading config from appsettings.json
var jwtConfig = Configuration.GetSection("MyApp:Jwt").Get<JwtConfig>();
//using config data ....
services.AddJwt(new AddJwtOptions(jwtConfig.JwtSecretConfigKey, jwtConfig.Issuer, jwtConfig.Audience, jwtConfig.ExpireMinutes));
var jwtConfig = Configuration.GetSection("MyApp").GetSection("Jwt").Get<JwtConfig>();