Get Multiple Connection Strings in appsettings.json without EF

前端 未结 3 740
天命终不由人
天命终不由人 2021-01-12 08:08

Just starting playing with the .Net Core RC2 by migrating a current MVC .Net app I developed. It looks like to me because of the way that configuration is handled with apps

相关标签:
3条回答
  • 2021-01-12 08:20

    There are a couple of corrections that I made to Adem's response to work with RC2, so I figured I better post them.

    I configured the appsettings.json and created a class like Adem's

    {
        "ConnectionStrings": {
          "DefaultConnectionString": "Default",
          "CustomConnectionString": "Custom"
        }
    }
    

    and

    public class ConnectionStrings
    {
        public string DefaultConnectionString { get; set; }
    
        public string CustomConnectionString { get; set; }
    }
    

    most of Adem's code comes out of the box in VS for RC2, so I just added the line below to the ConfigureServices method

    services.Configure<Models.ConnectionStrings>(Configuration.GetSection("ConnectionStrings"));
    

    The main missing point is that the connection string has to be passed to the controller (Once you’ve specified a strongly-typed configuration object and added it to the services collection, you can request it from any Controller or Action method by requesting an instance of IOptions, https://docs.asp.net/en/latest/mvc/controllers/dependency-injection.html)

    So this goes to the controller,

    private readonly ConnectionStrings _connectionStrings;
            public HomeController(IOptions<ConnectionStrings> connectionStrings)
            {
                _connectionStrings = connectionStrings.Value;
            }
    

    and then when you instantiate the DAL you pass the appropriate connectionString

    DAL.DataMethods dm = new DAL.DataMethods(_connectionStrings.CustomConnectionString);
    

    All the examples show this, they just don't state it, why my attempts to pull directly from the DAL didn't work

    0 讨论(0)
  • 2021-01-12 08:27

    I don't like the idea of instantiating the DAL. Rather, I'd do something like this

    public class ConnectionStrings : Dictionary<string, string> { }
    

    And something like this in the ctor of the DAL

    public Dal(IOptionsMonitor<ConnectionStrings> optionsAccessor, ILogger<Dal> logger)
    {
          _connections = optionsAccessor.CurrentValue;
          _logger = logger;
    }
    

    and you'll need to register with IoC

        services.Configure<ConnectionStrings>(configuration.GetSection("ConnectionStrings")); /* services is the IServiceCollection */
    

    Now you have all the connection strings in the DAL object. You can use them on each query or even select it by index on every call.

    0 讨论(0)
  • 2021-01-12 08:41

    You can use Options to access in DAL layer. I will try to write simple example(RC1):

    First you need to create appsettings.json file with below content:

    {
        "ConnectionStrings": {
          "DefaultConnectionString": "Default",
          "CustomConnectionString": "Custom"
        }
    }
    

    Then create a class:

    public class ConnectionStrings
    {
        public string DefaultConnectionString { get; set; }
    
        public string CustomConnectionString { get; set; }
    }
    

    And in Startup.cs

        private IConfiguration Configuration;
        public Startup(IApplicationEnvironment app)
        {
            var builder = new ConfigurationBuilder()
               .SetBasePath(app.ApplicationBasePath)
               .AddJsonFile("appsettings.json");
    
            Configuration = builder.Build();
        }
        public void ConfigureServices(IServiceCollection services)
        {
            // ....
            services.AddOptions();
            services.Configure<ConnectionStrings>(Configuration.GetSection("ConnectionStrings"));
        }
    

    Finally inject it in the DAL class:

        private IOptions<ConnectionStrings> _connectionStrings;
        public DalClass(IOptions<ConnectionStrings> connectionStrings)
        {
            _connectionStrings = connectionStrings;
        }
        //use it
    
    0 讨论(0)
提交回复
热议问题