从不同项目的appsettings.json中读取多个连接字符串

时间:2019-03-14 07:39:38

标签: c# asp.net-core .net-core asp.net-core-webapi core-api

我正在使用.Net Core v 2.1创建Web API。我的解决方案包含不同的项目层(BLL,DAL,Common等),并且主项目中有一个appsettings.json文件。我的appsettings.json文件中有多个连接字符串,我想根据控制器中传递的参数选择连接字符串。

这是项目结构和代码:

1)api层

appsettings.json

"ConnectionStrings": {
    "CON1": "con1 connectionstring",
    "CON2": "con2 connectionstring",
    "CON3": "con3 connectionstring"
},

Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    services.AddSingleton<IConfiguration>(Configuration);
}

例如我正在传递conn =“ CON1”

AccountController.cs

[HttpPost]
[Route("CreateUser")]
public IActionResult CreateUser(string conn, string username)
{
    try
    {
        AccountDL objAccountDL = new AccountDL(); //call account data layer
        objAccountDL.CreateUser(conn, username); //conn = "CON1"
        return Ok();
    }
    catch(Exception ex)
    {
        throw ex;
    }
}

2)dal层

AccountDL.cs

public class AccountDL
{
    IConfiguration _configuration;
    OracleConnection _oracleConnection;
    public string CreateUser(string conn, string username)
    {
        AppConfiguration appConfg = new AppConfiguration(_configuration);   
        _oracleConnection = appConfg.GetConnection(conn);
    }
}

3)通用配置层

AppConfiguration.cs

public class AppConfiguration
{
    public IConfiguration _configuration { get; }

    public AppConfiguration(IConfiguration configuration)
    {
        _configuration = configuration;
    }

    public OracleConnection GetConnection(string conn)
    {
        try
        {
            string connectionString = _configuration.GetSection("ConnectionStrings").GetSection(conn).Value;
            OracleConnection dbConn = new OracleConnection(connectionString);
            return dbConn;
        }
        catch(Exception ex)
        {
            throw ex;
        }
    }
}

现在,我将使用此dbConn连接对象进行进一步处理,但是我将Exception作为System.NullReferenceException:'对象引用未设置为对象的实例。'

如果我在同一个api层中尝试所有这些操作,那么我将基于传递的参数获取连接字符串,但是在重组我的项目后,我想在其他项目(即DAL)中使用此连接,如何获取连接字符串根据不同项目中的参数? 预先谢谢你。

1 个答案:

答案 0 :(得分:1)

因此,您可以从appsettings.json文件访问GetConnection()这样的方法的连接字符串值-

AppConfiguration.cs

public OracleConnection GetConnection(string conn)
{
    try
    {
        string connectionString = _configuration["ConnectionStrings:" + conn];
        OracleConnection dbConn = new OracleConnection(connectionString);
        return dbConn;
    }
    catch(Exception ex)
    {
        throw ex;
    }
}

AccountController.cs 控制器中,您需要在该控制器的构造函数中注入IConfiguration对象的依赖项。

public class AccountController
{
    public IConfiguration _configuration { get; }

    public AccountController(IConfiguration configuration)
    {
        _configuration = configuration;
    }

    [HttpPost]
    [Route("CreateUser")]
    public IActionResult CreateUser(string conn, string username)
    {
        try
        {
            AccountDL objAccountDL = new AccountDL(_configuration); //call account data layer
            objAccountDL.CreateUser(conn, username); //conn = "CON1"
            return Ok();
        }
        catch(Exception ex)
        {
            throw ex;
        }
    }
}

,因此,您还需要修改 AccountDL.cs 类。您需要在构造函数或方法参数中传递该对象。

public class AccountDL
{
    IConfiguration _configuration;
    OracleConnection _oracleConnection;

    public AccountDL(IConfiguration configuration)
    {
        _configuration = configuration;
    }

    public string CreateUser(string conn, string username)
    {
        AppConfiguration appConfg = new AppConfiguration(_configuration);   
        _oracleConnection = appConfg.GetConnection(conn);
    }
}