ASP.NET5配置

ASP.NET5支持各類各樣的配置,應用程序配置數據能夠來自JSON, XML或者INI格式的文件,也能來自環境變量,你也能夠自定義你本身的Configuration Provider.數據庫

1. 獲取和設置配置文件json

簡單地,Configuration類是一個源的集合,它能夠提供讀或寫鍵值對,你至少配置一個源,以下例所示:app

var builder = new ConfigurationBuilder();框架

builder.Add(new MemoryCOnfigurationSource());ide

var config = builder.Build();ui

config.Set("somekey", "somevalue");this

string setting = config.Get("somekey");code

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))

    {

      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();

  }

}

相關文章
相關標籤/搜索