0
我嘗試爲我的ASP.NET Core 2.0應用程序設置「dev」和「prod」環境,但它不起作用...下面是我的配置:配置ASP.NET核心應用程序的「prod」和「dev」
的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
(位於Users文件夾)的內容
{
"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
連接字符串都工作...
是否有解決問題的方法?謝謝 – Serge
@Serge只需選擇適當的配置源並定義設置即可。由於您已經將env變量用作其中一個源('builder.AddEnvironmentVariables()'),並且conn字符串是一個敏感信息,因此它可能是一個很好的選擇 - >添加一個與您的設置同名的env變量並設置conn字符串,因爲它的值。如果您部署到Azure,請考慮使用它們的Key-Value存儲。一般來說,你甚至可以把它放到另一個加密的配置文件中。查看[數據保護API](https://docs.microsoft.com/en-us/aspnet/core/security/data-protection)瞭解更多信息。 – Set
我的問題是相應的環境變量已經存在於服務器上,但不是在azure上更新它,而是嘗試在'appsetings.json'' dev'和'prod'中更新它們,這對部署和在Azure應用程序中運行:「),所以一旦我修改它在天藍色的相應插槽應用程序設置它的工作就像一個魅力。 – Serge