Asp.net core 配置ASP.NET核心应用程序的“prod”和“dev”
我尝试为我的ASP.NET Core 2.0应用程序设置开发和生产环境,但它不起作用。。。这是我的配置: appsettings.json内容与appsettings.Development.json完全相同 对于appsettings.Production.json,只需更改连接字符串和域名:Asp.net core 配置ASP.NET核心应用程序的“prod”和“dev”,asp.net-core,asp.net-core-2.0,asp.net-core-configuration,Asp.net Core,Asp.net Core 2.0,Asp.net Core Configuration,我尝试为我的ASP.NET Core 2.0应用程序设置开发和生产环境,但它不起作用。。。这是我的配置: appsettings.json内容与appsettings.Development.json完全相同 对于appsettings.Production.json,只需更改连接字符串和域名: "MyAppContext":"...AccountName=MyApptables4dev;AccountKey=yyyyyy==;EndpointSuffix=core..." 及 Startup
"MyAppContext":"...AccountName=MyApptables4dev;AccountKey=yyyyyy==;EndpointSuffix=core..."
及
Startup.cs:
最后是访问DB 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在生产环境中为空,可能问题就出在这里,但我不知道如何使它同时适用于MyAppContext和MyAppTablesConnectionString连接字符串…因此,您的代码从MyAppTablesConnectionString设置中读取值: 但根据提供的信息,只有dev env通过secret.json定义了此设置,并由builder=builder.AddUserSecrets;读取 因此,是的,生产环境为空,因为该设置未在任何配置源中定义: 用户机密未与prod一起使用 appsettings文件不描述MyAppTablesConnectionString 您不需要通过env变量传递这个值。
你有办法解决它吗?Thanks@Serge只需选择适当的配置源并定义设置。由于您已经将环境变量用作源builder.AddenEnvironmentVariables之一,并且conn字符串是敏感信息,因此它可能是一个不错的选项->添加与设置同名的环境变量,并将conn字符串设置为其值。如果部署到Azure,请考虑使用其键值存储。通常,您甚至可以将其放入另一个加密的配置文件中。查看更多信息。我的问题是服务器上已经存在相应的环境变量,但我没有在azure上更新它,而是尝试在appsetings.json dev和prod中更新它们,这对在Azure应用程序中部署和运行有任何影响:所以,一旦我在Azure相应的插槽应用程序设置中对其进行了修改,它就像一个符咒一样工作。
"Domain": "MyAppapp.azurewebsites.net",
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>();
// ...
{
"MyAppTablesConnectionString": "DefaultEndpointsProtocol=https;AccountName=myapptables4dev;AccountKey=xxx==;EndpointSuffix=core.windows.net"
}
public class TableClientOperationsService : ITableRepositories
{
// ...
public TableClientOperationsService() { }
public TableClientOperationsService(IOptions<AppSecrets> optionsAccessor)
{
tables = new Dictionary<string, CloudTable>();
// 'null' in 'prod'!
connectionString = optionsAccessor.Value.MyAppTablesConnectionString;
}
connectionString = optionsAccessor.Value.MyAppTablesConnectionString;