ASP.NET vNext global config access

后端 未结 2 777
清酒与你
清酒与你 2021-01-22 20:40

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 clas

相关标签:
2条回答
  • 2021-01-22 20:59

    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..
    }
    
    0 讨论(0)
  • 2021-01-22 21:01

    Here's what I've got that's working:

    Startup

    public class Startup
    {
        public IConfiguration Configuration { get; set; }
    
        public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
        {
            // create & store the configuration once
            var configurationBuilder = new ConfigurationBuilder(appEnv.ApplicationBasePath)
                .AddJsonFile("config.json")
                .AddEnvironmentVariables();
    
            Configuration = configurationBuilder.Build();
        }
    
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddEntityFramework()
                .AddSqlServer()
                .AddDbContext<Context>(options => options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
    
            services.AddIdentity<User, IdentityRole>()
                    .AddEntityFrameworkStores<Context>()
                    .AddDefaultTokenProviders();
    
            services.AddMvc();
    
            // adding/registering the dbContext for dependency injection as a singleton
            services.AddSingleton(s => new Context(Configuration));
        }
    
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            app.UseIdentity();
    
            app.UseMvc(routes =>
            {
                routes.MapRoute(
                      name: "default",
                      template: "{controller}/{action}/{id?}",
                      defaults: new { controller = "Home", action = "Index" });
            });
        }
    }
    

    DbContext

    public sealed class Context : IdentityDbContext<IdentityUser>
    {
        private readonly IConfiguration _config;
        public DbSet<Client> Clients { get; set; }
    
        public Context(IConfiguration config)
        {
            // store the injected config
            _config = config;
        }
    
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.ForSqlServer().UseIdentity();
            base.OnModelCreating(modelBuilder);
        }
    
        protected override void OnConfiguring(EntityOptionsBuilder optionsBuilder)
        {
            // use the injected config
            optionsBuilder.UseSqlServer(_config.Get("Data:DefaultConnection:ConnectionString"));
    
            base.OnConfiguring(optionsBuilder);
        }
    }
    

    Controller

    [Route("api/[controller]")]
    public class TestController : BaseController
    {
        private readonly Context _context;
    
        // have the context injected
        public TestController(Context context)
        {
            _context = context;
        }
    
        [HttpGet]
        public ActionResult Get()
        {
            return new ObjectResult(_context.Clients.ToList());
        }
    }
    
    0 讨论(0)
提交回复
热议问题