ASP.NET vNext全局配置的访问 [英] ASP.NET vNext global config access

查看:161
本文介绍了ASP.NET vNext全局配置的访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是访问正确的/推荐的方式config.json 文件(或任何其他地方的配置存储)在ASP.NET vNext?

启动类,我设置的配置,像这样:

 公共类启动
{
    公共IConfiguration配置{搞定;组; }公共启动(IHostingEnvironment ENV,IApplicationEnvironment appEnv)
{
    VAR configurationBuilder =新ConfigurationBuilder(appEnv.ApplicationBasePath)
        .AddJsonFile(config.json)
        .AddEnvironmentVariables();    配置= configurationBuilder.Build();
}

不过,如果我需要在其他地方访问连接字符串,我怎么办呢?例如,在 OnConfiguring 的EF上下文的,我怎么获得连接字符串:

 保护覆盖无效OnConfiguring(EntityOptionsBuilder optionsBuilder)
{
    optionsBuilder.UseSqlServer(???);

我读过,你可以使用这样的:

  VAR配置= Startup.Configuration
    ??新配置()
        .AddJsonFile(config.json)
        .AddEnvironmentVariables();

但是,(一)启动不是静态及(b)你一定不想去每一个你需要时间重建配置设置 - 这是复制code无处不在它的使用

我也看到了,你应该使用依赖注入,但没有按链接'T完全告诉你如何去做。如果我的DbContext构造函数有一个参数注入,那么我怎么注入了成参BaseApiController?

这似乎真的像一个普通/简单的要求:在启动时的配置之后,如何在其他地方我访问配置?这应该在文件/例子比比皆是。


解决方案

首先,你应该避免您的注册数据库上下文作为一个单身。又路过周围的原始 IConfiguration 接口是不是一个好的做法。

在代替可以创建一个POCO类选项:

 公共类DbOptions
{
    公共字符串的ConnectionString {搞定;设置}
}

和使用中的config.json部分填充它在 ConfigureServices 方法:

<$p$p><$c$c>services.Configure<DbOptions>(Configuration.GetConfigurationSection(\"Data:DefaultConnection\"));

然后你可以把它注射到你的的DbContext (和控制器等):

 公共密封类语境:IdentityDbContext&LT; IdentityUser&GT;
{
    私人只读DbOptions _options;    公共DbSet&LT;客户&GT;客户{搞定;组; }    公共上下文(IOptions&LT; D​​bOptions&GT; optionsAccessor)
    {
        //存储注入选项
        _options = optionsAccessor.Options;
    }    //其他code ..
}

What is the correct/recommended way of accessing the config.json file (or wherever else config is stored) in ASP.NET vNext?

In the Startup class, I set up the config like so:

public class Startup
{
    public IConfiguration Configuration { get; set; }

public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
{
    var configurationBuilder = new ConfigurationBuilder(appEnv.ApplicationBasePath)
        .AddJsonFile("config.json")
        .AddEnvironmentVariables();

    Configuration = configurationBuilder.Build();
}

But then if I need to access the connection string elsewhere, how do I do it? For example, in the OnConfiguring of an EF context, how do I get the connection string:

protected override void OnConfiguring(EntityOptionsBuilder optionsBuilder)
{
    optionsBuilder.UseSqlServer( ??? );

I've read that you could use this:

var config = Startup.Configuration
    ?? new Configuration()
        .AddJsonFile("config.json")
        .AddEnvironmentVariables();

But (a) Startup is not static and (b) you surely don't want to go rebuilding the configuration setup every time you need it - that's duplicating code everywhere it's used.

I've also read that you should use Dependency Injection, but that link doesn't fully show you how to do it. If my DbContext constructor has an injected parameter, then how do I inject that into a parameterless BaseApiController?

This really seems like a common/simple requirement: After the configuration in Startup, how to I access that configuration elsewhere? This should in documentation/examples everywhere.

解决方案

In the first place, you should avoid registering your database context as a singleton. Also passing around the raw IConfiguration interface isn't a good practice.

In stead could create a POCO options class:

public class DbOptions
{
    public string ConnectionString { get; set }
}

And populate it in the ConfigureServices method using the section in the config.json:

services.Configure<DbOptions>(Configuration.GetConfigurationSection("Data:DefaultConnection"));

Then you can inject it into your DbContext (and in controllers, etc.):

public sealed class Context : IdentityDbContext<IdentityUser>
{
    private readonly DbOptions _options;

    public DbSet<Client> Clients { get; set; }

    public Context(IOptions<DbOptions> optionsAccessor)
    {
        // store the injected options
        _options = optionsAccessor.Options;
    }

    // other code..
}

这篇关于ASP.NET vNext全局配置的访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆