猿问

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

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


这是项目结构和代码:


1) api层


应用设置.json


"ConnectionStrings": {

    "CON1": "con1 connectionstring",

    "CON2": "con2 connectionstring",

    "CON3": "con3 connectionstring"

},

启动.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) 通用配置层


应用配置.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 连接对象进行进一步处理,但是我得到了 System.NullReferenceException: 'Object reference not set to an instance of an object。


如果我在同一个 api 层中尝试所有这些,那么我将根据传递的参数获取连接字符串,但是在重组我的项目后,我想在不同的项目中使用这个连接,即 DAL,我怎样才能获得基于参数的连接字符串在不同的项目中?先感谢您。


喵喔喔
浏览 151回答 1
1回答

明月笑刀无情

因此,您可以从 appsettings.json 文件中访问连接字符串值以获取GetConnection()这样的方法 -应用配置.cspublic OracleConnection GetConnection(string conn){&nbsp; &nbsp; try&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; string connectionString = _configuration["ConnectionStrings:" + conn];&nbsp; &nbsp; &nbsp; &nbsp; OracleConnection dbConn = new OracleConnection(connectionString);&nbsp; &nbsp; &nbsp; &nbsp; return dbConn;&nbsp; &nbsp; }&nbsp; &nbsp; catch(Exception ex)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; throw ex;&nbsp; &nbsp; }}在AccountController.cs控制器中,您需要IConfiguration在该控制器的构造函数中注入对象的依赖关系。public class AccountController{&nbsp; &nbsp; public IConfiguration _configuration { get; }&nbsp; &nbsp; public AccountController(IConfiguration configuration)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; _configuration = configuration;&nbsp; &nbsp; }&nbsp; &nbsp; [HttpPost]&nbsp; &nbsp; [Route("CreateUser")]&nbsp; &nbsp; public IActionResult CreateUser(string conn, string username)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; try&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AccountDL objAccountDL = new AccountDL(_configuration); //call account data layer&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; objAccountDL.CreateUser(conn, username); //conn = "CON1"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Ok();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; catch(Exception ex)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw ex;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}因此您还需要修改AccountDL.cs类。您需要在构造函数或方法参数中传递该对象。public class AccountDL{&nbsp; &nbsp; IConfiguration _configuration;&nbsp; &nbsp; OracleConnection _oracleConnection;&nbsp; &nbsp; public AccountDL(IConfiguration configuration)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; _configuration = configuration;&nbsp; &nbsp; }&nbsp; &nbsp; public string CreateUser(string conn, string username)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; AppConfiguration appConfg = new AppConfiguration(_configuration);&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; _oracleConnection = appConfg.GetConnection(conn);&nbsp; &nbsp; }}
随时随地看视频慕课网APP
我要回答