ASP.NET Core 2.0アプリケーションの "dev"と "prod"環境を設定しようとしましたが、動作しません...ASP.NETコアアプリケーションの "prod"と "dev"を設定する
"MyAppContext":"...AccountName=MyApptables4dev;AccountKey=yyyyyy==;EndpointSuffix=core..."
:
appsettings.Production.json
だけで、接続文字列とドメイン名の変更について(
appsettings.Development.json
とまったく同じ)
appsettings.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"
}
}
}
:ここに私のconfigがあります
と
"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"
}
そして最後に、私の中で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
接続文字列の両方で動作させる方法がわからない、「生産」に「ヌル」であることを知って...
これを修正する方法はありますか? Thanks – Serge
@ Sergeは適切な設定ソースを選択して設定を定義するだけです。すでにenv変数をソース( 'builder.AddEnvironmentVariables()')として使用し、conn文字列は機密情報ですので、あなたの設定と同じ名前のenv変数を追加して設定するのが良いオプションですそれは値としてconn文字列です。 Azureにデプロイする場合は、Key-Valueストレージの使用を検討してください。一般に、別の暗号化された構成ファイルに入れることさえできます。詳細については、[Data Protection API](https://docs.microsoft.com/en-us/aspnet/core/security/data-protection)を参照してください。 – Set
私の問題は、対応する環境変数が既にサーバー上に存在していたのですが、それを空白で更新する代わりに、 'appsetings.json'' dev'と 'prod'で更新しようとしました。 Azureアプリケーションで実行しています: ")ので、私は紺碧の対応スロットアプリケーションの設定でそれを修正した後、魅力的なように働いていました。 – Serge