【问题标题】:Configure the "prod" and "dev" of an ASP.NET Core application配置 ASP.NET Core 应用程序的“prod”和“dev”
【发布时间】:2017-10-11 10:07:07
【问题描述】:

我尝试为我的 ASP.NET Core 2.0 应用程序设置“dev”和“prod”环境,但它不起作用...这是我的配置:

appsettings.json的内容(和appsettings.Development.json完全一样)

{
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": { "Default": "Warning" }
  },
  "ConnectionStrings": {
    "MyAppContext":     
      "...AccountName=MyApptables4dev; AccountKey=xxxxxx==;EndpointSuffix=core.windows.net"
  },
  "Authentication": {
    "AzureAd": {
      "AADInstance": "https://login.microsoftonline.com/",
      "CallbackPath": "/signin-oidc",
      "ClientId": "xxxxxxx",
      "Domain": "MyAppapp-dev.azurewebsites.net",
      "TenantId": "yyyyyyy"
    }
  }
}

对于 appsettings.Production.json 只是连接字符串和域名更改:

"MyAppContext":"...AccountName=MyApptables4dev;AccountKey=yyyyyy==;EndpointSuffix=core..."

"Domain": "MyAppapp.azurewebsites.net",

Startup.cs

public Startup(IHostingEnvironment env)
{
    var builder = new ConfigurationBuilder()
        .SetBasePath(env.ContentRootPath)
        .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
        .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);

    if (env.IsDevelopment())
    {
        builder = builder.AddUserSecrets<Startup>();
    }

    builder.AddEnvironmentVariables();
    Configuration = builder.Build();
}

public void ConfigureServices(IServiceCollection services)
{
    // Adds services required for using options.
    services.AddOptions();

    // Register the IConfiguration instance which "ConnectionStrings" binds against.
    //services.Configure<ConnectionStrings>(Configuration.GetSection("ConnectionStrings"));
    services.Configure<AppSecrets>(Configuration);
    services.AddSingleton<ITableRepositories, TableClientOperationsService>();
    // ...

secrets.json(位于用户文件夹中)内容

{
  "MyAppTablesConnectionString": "DefaultEndpointsProtocol=https;AccountName=myapptables4dev;AccountKey=xxx==;EndpointSuffix=core.windows.net"
}

最后是访问数据库的类(在我的例子中是 AzureTables)

public class TableClientOperationsService : ITableRepositories
{
    // ... 

    public TableClientOperationsService() { }
    public TableClientOperationsService(IOptions<AppSecrets> optionsAccessor)
    {
        tables = new Dictionary<string, CloudTable>();            
        // 'null' in 'prod'!
        connectionString = optionsAccessor.Value.MyAppTablesConnectionString; 
    }

知道MyAppTablesConnectionString 在“生产”中是“null”,也许问题出在那儿,但我不知道如何使它与MyAppContextMyAppTablesConnectionString 连接字符串一起工作。 .

【问题讨论】:

    标签: asp.net-core asp.net-core-2.0 asp.net-core-configuration


    【解决方案1】:

    所以您的代码从MyAppTablesConnectionString 设置中读取值:

    connectionString = optionsAccessor.Value.MyAppTablesConnectionString; 
    

    但根据提供的信息,只有 dev env 具有此设置(通过 secret.json 定义并由 builder = builder.AddUserSecrets&lt;Startup&gt;(); 读取)。

    是的,生产环境得到了null,因为该设置未在任何配置源中定义:

    • 用户密码不与 prod 一起使用
    • appsettings 文件不描述 MyAppTablesConnectionString
    • 并且您不会通过 env 变量传递此值。

    【讨论】:

    • 有解决方法的想法吗?谢谢
    • @Serge 只需选择适当的配置源并定义设置。由于您已经使用 env 变量作为源之一 (builder.AddEnvironmentVariables()) 并且 conn 字符串是敏感信息,因此它可能是一个不错的选择 -> 添加一个与您的设置同名的 env 变量并设置 conn 字符串价值。如果您部署到 Azure,请考虑使用其键值存储。通常,您甚至可以将其放入另一个加密的配置文件中。查看Data Protection API 了解更多信息。
    • 我的问题是服务器上已经存在相应的环境变量,但是我没有在 azure 上更新它,而是尝试在 appsetings.json devprod 中更新它们,这有对在 Azure 应用程序中部署和运行的任何影响 :") 所以一旦我在 azure 相应的插槽应用程序设置中对其进行了修改,它就像一个魅力。
    猜你喜欢
    • 2021-12-17
    • 2016-11-02
    • 1970-01-01
    • 1970-01-01
    • 2015-06-28
    • 1970-01-01
    • 2011-11-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多