ASP.NET5配置
ASP.NET5⽀持各种各样的配置,应⽤程序配置数据可以来⾃JSON, XML或者INI格式的⽂件,也能来⾃环境变量,你也可以⾃定义你⾃⼰的Configuration Provider.
1. 获取和设置配置⽂件
简单地,Configuration类是⼀个源的集合,它可以提供读或写键值对,你⾄少配置⼀个源,如下例所⽰:
var builder = new ConfigurationBuilder();
builder.Add(new MemoryCOnfigurationSource());
var config = builder.Build();
config.Set("somekey", "somevalue");
string setting = config.Get("somekey");
string setting2 = config["somekey"];
在Json⽂件中,key/value是通过:来分开的, 如果有多个层次时,键值也可以有:,如 Data:DefaultConnection:ConnectionString.
2. 使⽤内建的提供者
框架提供内建的多种配置⽂件源,如JSON, XML, INI. 也可以是环境变量,如下所⽰:
var builder = new ConfigurationBuilder(".");
builder.AddJsonFile("appsettings.json");
builder.AddEnvironmentVariables();
var config = builder.Build();
如果⼀个配置键存在多个配置⽂件当中,以配置顺序最晚的那个为准,上⾯的例⼦,以环境变量⾥变量为准,ASP.NET组建议把环境变量做为最后⼀个配置。
基于环境的配置⽂件可以如下配置:
public Startup(IHostingEnvironment env)
{
  var builder = new ConfigurationBuilder().AddJsonFile("appsettings.json")
    .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
}
在开发环境,配置⽂件为:appsettings.Development.json
3. 使⽤Options和Configuration对象
public class MyOptions
{
  public string Option1 {get;set;}
  public int Option2 {get;set;}
}
选项可以通过IOptions<TOptions>注⼊到你的应⽤程序当中去,例如:
public class HomeController : Controller
{
  public HomeController(IOptions<MyOptions> optionsAccessor)
}
配置Options如下:
public void ConfigureServices(IServiceCollection services)
{
  services.AddOptions();
  // Configure MyOptions using config
  services.Configure<MyOptions>(Configuration);
  // Confgiure MyOptions using code
  services.Configure<MyOptions>(myOptions => { myOptions.Option1 = "value1_from_action"; }); }
MyOptions.Option1可以配置在appsettings.json⽂件⾥⾯
4. ⾃定义providers
你可以写⼀个类继承⾃ConfigurationSource, 我们⽤EF来读取数据库的配置。
public class ConfigurationValue
{
  public string Id{get;set;}
  public string Value{get;set;}
}
public class ConfigurationContext : DbContext
{
  public COnfigurationContext(DbConextOptions options) : base(options) {}
  public DbSet<ConfigurationValue> Values{get;set;}
}
public class EntityFrameworkConfigurationSource : ConfigurationSource
{
  public EntityFrameworkConfigurationSource(Action<DbContextOptionsBuilder> optionsAction)  {
    OptionsAction = optionsAction;
  }
  Action<DbContextOptionsBuilder> OptionsAction {get;set;}
  public override void Load()
  {
    var builder = new DbContextOptionsBuilder<ConfigurationContext>();
    OptionsAction(builder);
    using(var dbContext = new ConfigurationContext(builder.Options))
aspnet和net的区别
    {
      dbContext.Database.EnsureCreated();
      Data = !dbContext.Values.Any() ? CreateAndSaveDefaultValues(dbContext)
        : dbContext.Values.ToDictionary( c=> c.Id, c=> c.Value);
    }
  }
  private IDictionary<string, string> CreateAndSaveDefaultValues(ConfigurationContext dbContext)
  {
    var configValues = new Dictionary<string, string>{...};
    dbContext.Values.AddRange(configValues.Select(kvp => new ConfigurationValue(){Id = kvp.Key, value = kvp.Value }).ToArray());    dbContext.SaveChanges();
    return configValues;
  }
}
public static class EntityFrameworkExtensions
{
  public static IConfigurationBuilder AddEntityFramework(this IConfigurationBuilder builder, Action<DbContextOptionsBuilder> setup)  {
    return builder.Add(new EntityFrameworkConfigurationSource(setup));
  }
}
public class Program
{
  public void Main(string[] args)
  {
    var builder = new ConfigurationBuilder(".");
    builder.AddJsonFile("appsettings.json");
    var config = builder.Build();
    builder.AddEntityFramework(options => options.UseSqlServer(config["Data:DefaultConnection:ConnectionString"]));
    config = builder.Build();
  }
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。