.NET Core 配置多个环境设置

appsettings.json

{
    "Logging": {
        "LogLevel": {
            "Default": "Warning"
        }
    },
    "AllowedHosts": "*",
    "spring": {
        "application": {
            "name": "octopus-cloud-net-user"
        }
    },
    "eureka": {
        "client": {
            "serviceUrl": "http://localhost:9871/eureka/",
            "shouldRegisterWithEureka": true,
            "shouldFetchRegistry": false,
            "validateCertificates": false
        },
        "instance": {
            "hostName": "localhost"
        }
    },
    "apollo": {
        "AppId": "netcore-config-test",
        "MetaServer": "http://192.168.0.52:8080"
    }
}

appsettings.Development.json

{
    "Logging": {
        "LogLevel": {
            "Default": "Warning"
        }
    },
    "AllowedHosts": "*",
    "eureka": {
        "instance": {
            "port": 9102,
            "instanceId": "octopus-cloud-net-user:9102"
        }
    }
}

appsettings.TestD.json

{
    "Logging": {
        "LogLevel": {
            "Default": "Warning"
        }
    },
    "AllowedHosts": "*",
    "eureka": {
        "instance": {
            "port": 9103,
            "instanceId": "octopus-cloud-net-user:9103"
        }
    }
}

上面文件名中的 DevelopmentTestD 就是环境变量。

Properties/launchSettings.json

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:9102",
      "sslPort": 0
    }
  },
  "$schema": "http://json.schemastore.org/launchsettings.json",
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "api/user",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "Octopus.Cloud.User": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "api/user",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "applicationUrl": "http://localhost:9102"
    },
    "TestD": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "api/user",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "TestD"
      },
      "applicationUrl": "http://localhost:9103"
    }
  }
}

通过 dotnet run 启动程序,参数中的 --launch-profile TestD 则是指定 launchSettings.json 中的 profiles.TestD

dotnet run --launch-profile TestD

参考

  1. dotnet run 命令
  2. 在 ASP.NET Core 中使用多个环境

© 2018, 佳佳. 版权所有. 转载请注明出处.

发表评论

电子邮件地址不会被公开。 必填项已用*标注