簡體   English   中英

在 Azure 中為 ASP.NET Core Web 應用設置 SQL 連接字符串

[英]Setting the SQL connection string for ASP.NET Core web app in Azure

我在 Visual Studio 2015 中創建了一個新的 ASP.NET Core Web 應用程序。我還設置了一個 Azure Web 應用程序以從 GitHub 中提取應用程序並運行它。 這工作正常,但我無法連接到 Azure 上的數據庫。

在本地,這是有效的,它使用config.json和代碼Data:DefaultConnection:ConnectionString作為連接字符串。

我怎樣才能讓代碼保持原樣,並讓它也能在 Azure 中運行? 我嘗試在門戶中設置應用程序設置,包括連接字符串和應用程序設置。 並同時使用“SQLCONNSTR_DefaultConnection”和“Data:DefaultConnection:ConnectionString”作為鍵。

(順便說一下設置App Settings好像不行。我覺得我提供的值太長了)。

那么如何將我的 Azure 數據庫的連接字符串提供給我的 Azure Web 應用程序 (ASP.NET 5),而無需在源代碼管理中將其簽入?

更新我的 Startup.cs 看起來像這樣(請參閱GitHub 上的完整文件):

public Startup(IHostingEnvironment env)
{
    var configuration = new Configuration()
        .AddJsonFile("config.json")
        .AddJsonFile($"config.{env.EnvironmentName}.json", optional: true);

    if (env.IsEnvironment("Development"))
    {
        configuration.AddUserSecrets();
    } 

    configuration.AddEnvironmentVariables();
    Configuration = configuration;
}

ConfigureServices方法中,還有:

services.Configure<AppSettings>(Configuration.GetSubKey("AppSettings"));

並且,也在ConfigureServices方法中:

services.AddEntityFramework()
            .AddSqlServer()
            .AddDbContext<ApplicationDbContext>(options => 
                   options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]))
            .AddDbContext<InvoicesDbContext>(options => 
                   options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
// So this is where I want my app in Azure to use the connection string I
// provide in the portal

簡答

我嘗試在門戶中設置應用程序設置,包括連接字符串和應用程序設置。 並同時使用“SQLCONNSTR_DefaultConnection”和“Data:DefaultConnection:ConnectionString”作為鍵。

你很接近。

  1. 轉到 Azure Web 應用 > 配置 > 連接字符串。
  2. 添加名為DefaultConnection的連接字符串。
  3. 使用Configuration.Get("Data:DefaultConnection:ConnectionString")來訪問它。

使用timesheet_db而不是DefaultConnection示例

這是我自己的時間表應用程序中的一個示例。 我的連接字符串被命名為timesheet_db 只需用DefaultConnection替換該字符串的所有實例即可使示例適應您的用例。

Azure Web 應用配置

設置連接字符串

Azure Web 應用服務控制管理器

https://myWebAppName.scm.azurewebsites.net/Env 上的在線服務控制管理器將顯示連接字符串。

連接字符串

啟動文件

Startup設置配置設置,以便環境變量覆蓋 config.json。

public IConfiguration Configuration { get; set; }
public Startup()
{
    Configuration = new Configuration()
        .AddJsonFile("config.json")
        .AddEnvironmentVariables();    <----- will cascade over config.json
}

Startup配置數據庫。

public void ConfigureServices(IServiceCollection services)
{
    services
        .AddEntityFramework()
        .AddSqlServer()
        .AddDbContext<ProjectContext>(options =>
        {
            var connString =
                Configuration.Get("Data:timesheet_db:ConnectionString");
            options.UseSqlServer(connString);
        });
}

當然,該示例使用名為timesheet_db的連接字符串。 對您來說,用您自己的名為DefaultConnection的連接字符串替換它的所有實例,一切都會正常工作。

在 RC2 中,我必須更改我的連接字符串的讀取方式才能讓它們在 Azure 中工作。 就我而言,我必須確保 Azure 連接字符串被命名為“DefaultConnection”,並通過以下方式訪問:

RC1:

{
    "Data": {
        "DefaultConnection": {
            "ConnectionString": "Server=(localdb)\\MSSQLLocalDB;Database=db;Trusted_Connection=True;"
        }
    }
}

訪問者:

var conn = Configuration["Data:DefaultConnection:ConnectionString"];

RC2:

{
  "Data": {

  },
  "ConnectionStrings": {
    "DefaultConnection": "Server=(localdb)\\MSSQLLocalDB;Database=db;Trusted_Connection=True;"
  }
}

訪問者:

var conn = Configuration.GetConnectionString("DefaultConnection");

您有許多選項可以設置連接字符串。 默認設置類從不同來源獲取環境設置。 您可以在 config.production.json 中設置您的連接字符串。 或 config.staging.json。 查看啟動類

    public Startup(IHostingEnvironment env)
    {
        // Setup configuration sources.
        var configuration = new Configuration()
            .AddJsonFile("config.json")
            .AddJsonFile($"config.{env.EnvironmentName}.json", optional: true);

        if (env.IsEnvironment("Development"))
        {
            // This reads the configuration keys from the secret store.
            // For more details on using the user secret store see http://go.microsoft.com/fwlink/?LinkID=532709
            configuration.AddUserSecrets();
        }
        configuration.AddEnvironmentVariables();
        Configuration = configuration;
    }

我認為您正在尋找SlotSticky設置

在 Azure PowerShell 中使用此命令將 2 個應用設置設置為粘性到插槽

Set-AzureWebsite -Name mysite -SlotStickyAppSettingNames @("myslot", "myslot2")

並且此命令將 2 個連接字符串設置為粘到插槽

Set-AzureWebsite -Name mysite -SlotStickyConnectionStringNames @("myconn", "myconn2")

暫無
暫無

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

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