繁体   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