簡體   English   中英

在 Asp.Net Core 應用程序中使用連接字符串的最佳實踐

[英]Best Practice to use connection string in Asp.Net Core app

我不能使用 EF,因為它不適用於 Oracle(或者我發現的教程已經過時且無法使用)。

我打算使用 ODP.Net 或 devart 的驅動程序。

使用 appsettings.json 中的連接字符串的最佳方法是什么?

我看過很多教程不完整或不適用於 Asp.Net Core 3.1

我的控制器

public class ReportController : Controller
{
    private readonly ConnectionString myString;
    public ReportController(IOptions<ConnectionString> connectionString)
    {
        myString = connectionString.Value;
    }

    public ActionResult Reporting_Totals()
    {
        string connectionString = myString.ToString();
        DataTable dt = new DataTable();
        using (OracleConnection oracleConnection = new OracleConnection())
        return View();
    }

連接字符串類

public class ConnectionString
{        
    private string connectionString { get; set; }
    // enable set in singleton property instance to work correctly.
    //public static ConnectionString Instance { get; set; } = new ConnectionString();

    //public string DatabaseConnection { get; set; }
}

我的創業班

    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
        //configuration.GetSection("ConnectionString").Bind(ConnectionString.Instance);
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<ConnectionString>(Configuration.GetSection("ConnectionStrings"));
        services.AddControllersWithViews();
    }

我的 appsettings.json

  "ConnectionStrings": {
    "DEV": "Server=myServer;Database=myDb1;Trusted_Connection=True;",
    "PROD": "Server=myServer;Database=myDb2;Trusted_Connection=True;"

是否有任何特殊原因需要特殊的ConnectionString class 如果只想獲取連接字符串,可以直接注入IConfiguration ,只需使用GetConnectionString擴展方法。

public class ReportController
{
   private readonly string myString;

   public ReportController(IConfiguration configuration)
   {
      myString = configuration.GetConnectionString("DEV");
   }
}

/*
appsettings.json
{
   "ConnectionStrings": {
      "DEV": "<Your connection string>",
      "PROD": "<Your connection string>"
   }
}
*/

這樣做的好處是,只要您可以通過.NET Core的配置系統加載配置,無論連接字符串存儲在哪里,您都可以使用此方法。 因此,如果您決定使用其他人建議的 Azure Key Vault 或 AWS,或使用 appsettings.json 來存儲您的連接字符串,您可以注入IConfiguration並使用GetConnectionString ,只要有一個配置提供程序用於存儲您的連接字符串秘密。 .json 文件和 Azure Key Vault 已經存在這樣的提供程序,我不知道 AWS。

另一方面,您可以查看在 ASP.NET Core使用多個環境並為不同的環境使用不同的配置設置。 這樣,您可以使用“Reports”或其他一些描述您正在連接的數據源的名稱,而不是“DEV”和“PROD”作為“ConnectionStrings”配置中的鍵,並具有不同的名稱您給定環境的連接字符串。

例如,使用 appsettings.{Environment}.json 文件...

public class ReportController
{
   private readonly string myString;

   public ReportController(IConfiguration configuration)
   {
      myString = configuration.GetConnectionString("Reports"); //Will give you the correct connection string based on your environment.
   }
}

/*
appsettings.Production.json
{
   "ConnectionStrings": {
      "Reports": "<Your production connection string>",
   }
}

appsettings.Development.json
{
   "ConnectionStrings": {
      "Reports": "<Your development connection string>"
   }
}
*/

我相信最好的方法是使用依賴注入:

public void ConfigureServices(IServiceCollection services)
{
    services.Configure<ConnectionString>(Configuration.GetSection(nameof(ConnectionStrings))AddOptionsSnapshot<ConnectionStrings>();
    services.AddControllersWithViews();
}

然后在控制器上:

internal ConnectionStrings ConnectionStrings
{
    get
    {
        return this.HttpContext.RequestServices.GetRequiredService<ConnectionStrings>();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM