简体   繁体   English

如何使用.Net core 5在任何Class中读取appsettings.json

[英]How to read appsettings.json in any Class with .Net core 5

I have this class that connects to my oracle database:我有这个 class 连接到我的 oracle 数据库:

public class Conexao : IDisposable
{
    private readonly IOptions<Configuracao> _config;
    public OracleConnection ConexaoOra{ get; internal set; }
    private OracleTransaction _transacao;
    private bool _finalized;
    private bool _commited;
    private const string CONNECTION_STRING = "myConnectionString";


    public Conexao(IOptions<Configuracao> config)
    {
        _config = config;
        _finalized = false;
        _commited = false;
        ConexaoOra = new OracleConnection();
        ConexaoOra.ConnectionString = CONNECTION_STRING;
        ConexaoOra.Open();
    }

  
    ~Conexao()
    {
        Dispose();
    }

    public void BeginTransaction(System.Data.IsolationLevel level = System.Data.IsolationLevel.ReadCommitted)
    {
        _transacao = ConexaoOra.BeginTransaction(level);
    }

    public void Commit()
    {
        if (_transacao != null)
        {
            _transacao.Commit();
            _transacao = null;
        }
        _commited = true;
    }

}

This class is instantiated every time any transaction is triggered in the DAL class每次在 DAL class 中触发任何事务时,都会实例化此 class

sample:样本:

   using ((conexao == null ? conexao = new Conexao() : conexao))
   OracleCommand oracleCommand = new OracleCommand("", conexao.ConexaoOra);
   //... 

The Json Settings File and the Connection Class are in different projects in the same solution Json 设置文件和连接 Class 在同一解决方案的不同项目中

how to load in this line my connectionString of appSettings Json File?如何在此行中加载我的 appSettings Json 文件的连接字符串?

       private const string CONNECTION_STRING = "myConnectionString";

Sorry, I took the example from an old project where I used ServiceStack and it probably had some helpers to ease the procedure.抱歉,我从一个使用 ServiceStack 的旧项目中举了一个例子,它可能有一些帮助程序来简化程序。 With just plain .NET it seems to be more tedious, so have a look here and here .仅使用普通的 .NET 似乎更乏味,所以看看这里这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM