【问题标题】:Publish not getting connection string from appsettings.production.json发布未从 appsettings.production.json 获取连接字符串
【发布时间】:2016-12-22 15:50:21
【问题描述】:

我无法让已发布的 ASP.net Core 应用程序切换到使用 appsettings.production.json 文件中的连接字符串。

我已设置 launchSettings 以将 ASPNETCORE_ENVIRONMENT 环境变量用于我的开发和生产配置文件。当我切换配置文件并在 Visual Studio 中运行它们时,我的连接字符串会正确更改。

当我在我的 Ubuntu 服务器上运行已发布的应用程序时,它不会切换。我已在我的服务器上将 ASPNETCORE_ENVIRONMENT 变量设置为“生产”。我还验证了 appSettings.json 和 appSettings.production.json 都存在于应用程序的根目录中。

我的 appsettings.json 文件:

{
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
    }
  },
  "ConnectionStrings": {
    "DefaultConnection": "server=localhost;user id=root;password=dev;database=testdb;sslmode=none",
  }
}

我的 appsettings.production.json 文件:

{
  "ConnectionStrings": {
    "DefaultConnection": "server=localhost;user id=root;password=prod;database=testdb;sslmode=none"
  }
}

我的 launchSettings.json 文件:

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:50824/",
      "sslPort": 0
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "home/index",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "IIS Express (Production)": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "home/index",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Production"
      }
    }
  }
}

我的 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);

    if (env.IsEnvironment("Development"))
    {
        // This will push telemetry data through Application Insights pipeline faster, allowing you to view results immediately.
        builder.AddApplicationInsightsSettings(developerMode: true);
    }

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


    Console.WriteLine("Root Path: " + env.ContentRootPath);
    Console.WriteLine("Connection String: " + Configuration.GetConnectionString("DefaultConnection"));
}

我已经参考了这些问题,但没有运气:

asp.net core 1 appsettings.production.json not updating connection strings

dotnet publish doesn´t publish correct appsettings.{env.EnvironmentName}.json

【问题讨论】:

    标签: asp.net-core


    【解决方案1】:

    事实证明,official documentation 中的这个“注释”非常重要:

    在 Windows 和 macOS 上,指定的环境名称是 case 不敏感。是否将变量设置为 Development 或 开发或开发的结果将是相同的。然而, 默认情况下,Linux 是区分大小写的操作系统。环境变量,文件 名称和设置应区分大小写以实现最佳实践。

    主要是“Linux 是默认区分大小写的操作系统”这一行!!!!!!!哎呀:)

    一旦我将环境变量更改为“生产”而不是“生产”,它就起作用了。

    进一步说明:

    关键是理解Startup.cs启动方法中的这行代码:

    .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
    

    它将 {env.EnvironmentName} 替换为您的环境变量,因此如果您在 linux 中操作,它需要与您的文件名完全匹配。在我的例子中是“appSettings.production.json”,所以 ASPNETCORE_ENVIRONMENT 必须是“production”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-10
      • 1970-01-01
      • 2016-11-14
      相关资源
      最近更新 更多