Passing application's connection string down to a Repository Class Library in ASP.NET 5 using the IConfigurationRoot

后端 未结 7 1754
情话喂你
情话喂你 2020-12-08 19:51

I have an ASP.NET 5 MVC Web Application and in Startup.cs I see that the public property

IConfigurationRoot Configuration 

is being set to

相关标签:
7条回答
  • 2020-12-08 20:31

    I have a constructor in my repository class that accepts the db connection string as a parameter. This works for me when I add my repository for injection. In ConfigureServies() of the startup.cs file add this:

    services.AddScoped<IRepos>(c => new Repos(Configuration["DbConnections:ConnStr1"]));
    

    IRepos.cs is the interface, Repos.cs is the class that implements it. And of course Configuration is just a reference to the built IConfigurationRoot object.

    0 讨论(0)
  • 2020-12-08 20:33

    If ConfigureServices in your project's startUp.cs contains

    services.AddEntityFramework()
                 .AddSqlServer()
                 .AddDbContext<YourDbContext>(options =>
                     options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
    

    and your repository cs file is having constructor injection as shown below

    public class MyRepo : IRepo
    {
        private readonly YourDbContext dbContext;
        public MyRepo(YourDbContext ctx)
        {
            dbContext = ctx;
        }
    }
    

    YourDbContext will be automatically resolved.

    0 讨论(0)
  • 2020-12-08 20:42

    A slightly different approach would be to make a static class in your Class Library on which you call a method from the Configure(..)-method in Startup.cs:

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        ...
        ConnectionManager.SetConfig(Configuration);
    }
    

    In this case, I've added Configuration as a Singleton in ConfigureServices:

    services.AddSingleton(_ => Configuration);
    

    My ConnectionManager looks like this:

    public class ConnectionManager
    {
        private static IConfiguration currentConfig;
    
        public static void SetConfig(IConfiguration configuration)
        {
            currentConfig = configuration;
        }
    
        /// <summary>
        /// Get a connection to the database.
        /// </summary>
        public static SqlConnection GetConnection
        {
            get
            {
                string connectionString = currentConfig.GetConnectionString("MyConnection");
                // Create a new connection for each query.
                SqlConnection connection = new SqlConnection(connectionString);
                return connection;
            }
        }
    }
    

    This may or may not have some issues regarding object lifetimes and such, and I'm certainly no fan of static classes but as far as I can tell it's a viable approach. Instead of passing Configuration you could even extract the ConnectionString from the config-file and send only that.

    0 讨论(0)
  • 2020-12-08 20:42

    What you need is to create a class in class library project to access the appsettings.json in website project and return connection string.

    {
        private static string _connectionString;
    
        public static string GetConnectionString()
        {
            if (string.IsNullOrEmpty(_connectionString))
            {
                var builder = new ConfigurationBuilder()
               .AddJsonFile("appsettings.json");
                Configuration = builder.Build();
                _connectionString = Configuration.Get<string>("Data:MyDb:ConnectionString");
            }
            return _connectionString;
        }
        public static IConfigurationRoot Configuration { get; set; }
    
    }
    
    0 讨论(0)
  • 2020-12-08 20:47

    on your Startup.cs file add the following method

    public void ConfigureServices(IServiceCollection services) {
        services.AddSingleton(_ => Configuration);
    }
    

    then update your BaseRepo class like this

    public class BaseRepo {
        private readonly IConfiguration config;
    
        public BaseRepo(IConfiguration config) {
            this.config = config;
        }
    
        public SqlConnection GetOpenConnection() {
            string cs = config["Data:DefaultConnection:ConnectionString"];
            SqlConnection connection = new SqlConnection(cs);
            connection.Open();
            return connection;
        }
    }
    
    0 讨论(0)
  • 2020-12-08 20:52

    ASP.NET provides its own way of passing around configuration settings.

    Suppose you have the this in your appSettings.json:

    {
      "Config": {
        "Setting1": 1,
        "Setting2": "SO"
      }
    }
    

    Then you need a class like this:

    public class MyConfiguration
    {
        public int Setting1 { get; set; }
    
        public string Setting2 { get; set; }
    }
    

    This allows you to configure your service with this configuration by adding the following line

    services.Configure<MyConfigurationDto>(Configuration.GetSection("Config"));
    

    to ConfigureServices.

    You can then inject the configuration in constructors by doing the following:

    public class SomeController : Controller
    {
        private readonly IOptions<MyConfiguration> config;
    
        public ServiceLocatorController(IOptions<MyConfiguration> config)
        {
            this.config = config;
        }
    
        [HttpGet]
        public IActionResult Get()
        {
            return new HttpOkObjectResult(config.Value);
        }
    }
    

    This example is for controllers. But you can do the same with other layers of you application.

    0 讨论(0)
提交回复
热议问题