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是在「生產」,「空」,也許問題來自那裏,但我不知道如何使它與MyAppContextMyAppTablesConnectionString連接字符串都工作...

回答

1

所以,你的代碼MyAppTablesConnectionString設置讀取值:

connectionString = optionsAccessor.Value.MyAppTablesConnectionString; 

但相應地提供信息,僅開發ENV有此設置(通過​​通過secret.json定義和讀取)。

所以,是的,生產環境得到了null,作爲設置不以任何配置源定義:

  • 用戶密不與督促使用
  • appsettings文件沒有描述MyAppTablesConnectionString
  • 並且您不通過env變量傳遞此值。
+0

是否有解決問題的方法?謝謝 – Serge

+0

@Serge只需選擇適當的配置源並定義設置即可。由於您已經將env變量用作其中一個源('builder.AddEnvironmentVariables()'),並且conn字符串是一個敏感信息,因此它可能是一個很好的選擇 - >添加一個與您的設置同名的env變量並設置conn字符串,因爲它的值。如果您部署到Azure,請考慮使用它們的Key-Value存儲。一般來說,你甚至可以把它放到另一個加密的配置文件中。查看[數據保護API](https://docs.microsoft.com/en-us/aspnet/core/security/data-protection)瞭解更多信息。 – Set

+1

我的問題是相應的環境變量已經存在於服務器上,但不是在azure上更新它,而是嘗試在'appsetings.json'' dev'和'prod'中更新它們,這對部署和在Azure應用程序中運行:「),所以一旦我修改它在天藍色的相應插槽應用程序設置它的工作就像一個魅力。 – Serge

相關問題