问题描述
的 asp.net mvc的6 beta5的的
我试图用config.json激活\\ disactive记录
I've tried to use config.json to activate\disactive logging
public IConfiguration Configuration { get; set; }
public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
{
var configurationBuilder = new ConfigurationBuilder(appEnv.ApplicationBasePath)
.AddJsonFile("config.json")
.AddEnvironmentVariables();
Configuration = configurationBuilder.Build();
DBContext.ConnectionString = Configuration.Get("Data:DefaultConnection:ConnectionString");
}
public void ConfigureServices(IServiceCollection services)
{
services.AddOptions();
services.Configure<AppSettings>(Configuration.GetConfigurationSection("AppSettings"));
}
// Configure is called after ConfigureServices is called.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerfactory)
{
// that line cause NullReferenceException
AppSettings settings = ConfigurationBinder.Bind<AppSettings>(Configuration);
if (settings.Log.IsActive)
{
................
}
从ASP.NET 5(vNext) - 获取一个配置设置和的
是否有另一种方式来获得的AppSettings的实例在配置的方法?我需要的类型的对象。
Example from ASP.NET 5 (vNext) - Getting a Configuration Setting and http://perezgb.com/2015/07/04/aspnet-5-typed-settings-with-the-configurationbinder/
Is there another way to get an instance of the AppSettings in the "configure" method? I need typed object.
推荐答案
在 ConfigureServices
配置的每个服务可以在配置被注入
运行时的方法:
Every service configured in ConfigureServices
can be injected in the Configure
method by the runtime:
public void Configure(IApplicationBuilder app, IOptions<AppSettings> options)
{
// access options.Options here
}
这是比访问有点清洁的解决方案的的ServiceProvider
直接
This is a bit cleaner solution than accessing the ServiceProvider
directly.
这篇关于如何使用ConfigurationBinder在startup.cs的配置方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!