azure - 我可以使用不同的 appsettings.xxx.json 文件和调试配置文件设置多个开发环境吗?

标签 azure asp.net-core asp.net-core-configuration

我阅读了一些有关设置 ASP.Net Core 应用程序部署环境的文档。这些文章通常引用开发暂存生产按名称命名,但绝不偏离这些传统环境名称。

通常,一旦您退出“开发”,您就会想要关闭开发/调试设置,以便在应用程序崩溃时敏感信息不会泄露到网络上。这是有道理的。

但是,我的应用程序处于开发的早期阶段,我需要两个可以调试的开发环境配置。具体来说,我的团队主要希望在本地进行开发,连接到本地 SQL Server 数据库。但是,我们需要设置和测试 Azure 数据库,对于初步设置,如果我们可以在本地运行服务器开发模式,并且能够从我们的开发盒连接到我们的 Azure 数据库,将会有所帮助。

我想做的是创建两个名为 aspsettings.Development.jsonaspsettings.LocalDevelopment.json 的配置文件,它们都在我的两个 ASP 中。我的解决方案中的 Net Core 项目——一个用于 Web API,另一个用于 UI 项目。

Development 将包含用于连接到正确的开发数据库服务器(用于需要访问 Azure 的开发测试的 Azure 数据库)的所有值,并且将使用 LocalDevelopment 环境用于连接到本地数据库。

我已将这些文件添加到我的项目中,将 Development 详细信息复制到 LocalDevelopment 中,并仅更改了 API 项目配置的连接字符串。

接下来,我打开项目属性并添加两个用于调试的配置文件。为了解决这个问题,我为 API 项目和 UI 项目创建了这些相同的配置文件。这些配置文件被命名为“IIS Local”,另一个被命名为“IIS Dev Server”。最后,在每个新配置文件的每个项目页面中,我输入了 ASPNETCORE_ENVIRONMENT-- DevelopmentLocalDevelopment 各自的值。

当我将应用程序调试为开发时,它工作正常。但是,当我使用 LocalDevelopment` 环境和配置文件运行应用程序时,出现以下错误:

Error. An error occurred while processing your request. Request ID: 0HLLE04D5NFDU:00000001

Development Mode Swapping to Development environment will display more detailed information about the error that occurred.

Development environment should not be enabled in deployed applications, as it can result in sensitive information from exceptions being displayed to end users. For local debugging, development environment can be enabled by setting the ASPNETCORE_ENVIRONMENT environment variable to Development, and restarting the application.

这似乎并不成立,因为这两个配置对于​​各自的项目来说是相同的,唯一的区别是 API 中的连接字符串,而且我确实添加了一个 EnvironmentName 属性来进行识别。

我可能做错了什么?

以下是LocalDevelopment 文件的内容。以防万一我遗漏了一些东西。

API 中的设置

{
  "Logging": {
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
    }
  },
  "EnvironmentName": "LOCAL",
  "ConnectionStrings": {
    "Database": "xxx"
  }
}

用户界面中的设置

{
  "Logging": {
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
    }
  }
}

最佳答案

在 Startup.cs 中,您的 Configure 方法中可能有类似以下内容:

if (env.IsDevelopment())
{
    app.UseDeveloperExceptionPage();
    app.UseDatabaseErrorPage();
}
else
{
    app.UseExceptionHandler("/error/500");
}

您需要将条件更改为:

if (env.IsDevelopment() || env.IsEnvironment("LocalDevelopment"))

或者您可以使用开发错误页面简单地创建任何非生产环境:

if (!env.IsProduction())

诸如 IsDevelopmentIsProduction 等方法只是语法糖,因此您不必执行 IsEnvironment("Development")。但是,由于 LocalDevelopment 是您自己创建的,因此显然没有为此内置的方法。

关于azure - 我可以使用不同的 appsettings.xxx.json 文件和调试配置文件设置多个开发环境吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55281975/

相关文章:

c# - 如何单元测试方法调用 IConfiguration.Get<T> 扩展

powershell - 如何在PowerShell中知道已安装的软件版本

c# - Asp net Core 获取用户Windows用户名

linux - 将数万个小文件移动到 Azure 存储容器的最快方法

c# - 在 .NET Core WebAPI 中使用分布式内存缓存时设置最大内存

asp.net-mvc - Linux 上的 aspnet core 1.0 完整的 .net 框架

c# - Configuration.GetSection 返回空值

c# - ASP.NET Core appsettings.json 未加载正确的设置

azurewebsites.net 内置证书

azure - Azure 搜索服务上的 CORS 问题