如何在不覆盖.NET Core内置配置的情况下启动我的自定义配置?

huangapple go评论70阅读模式
英文:

How do I launch my own configuration without overriding .NET Core's built-in configuration?

问题

我在Visual Studio 2022中创建了一个launchSettings.json文件,使我能够从Visual Studio的启动设置下拉菜单中快速更改配置,如数据库连接字符串。

由于launchsettings.json将ASPNETCORE_ENVIRONMENT用作选择关联appsettings的键,我无法使用.NET Core内置的配置,如RELEASE,STAGING和DEVELOPMENT。 因此,我的代码及其依赖项无法响应内置配置。

是否有解决方法?

英文:

I've created a launchSettings.json file in Visual Studio 2022, enabling me to quickly change configurations such as the database connection string from Visual Studio's launch settings dropdown.

LaunchSettings.json

"iisSettings": {

    "iisExpress": {

      "applicationUrl": "http://localhost",

      "sslPort": 44392

    }

  },

  "profiles": {

    “Client_1”: {

      "commandName": "IISExpress",

      "launchBrowser": true,

      "environmentVariables": {

        "ASPNETCORE_ENVIRONMENT": “Client_1_ENV

      }

    },

    “Client_2: {

      "commandName": "Project",

      "launchBrowser": true,

      "applicationUrl": "https://localhost:5001;http://localhost:5000",

      "environmentVariables": {

        "ASPNETCORE_ENVIRONMENT": "Client_2_ENV"

      }

    }

  }

}

appsettings.client_1_ENV.json

{
   “ConnectionStrings”: “db.client_1.com”
   "ASPNETCORE_ENVIRONMENT": "Development"
}

appsettings.client_2_ENV.json

{

   “ConnectionStrings”: “db.client_2.com”
   "ASPNETCORE_ENVIRONMENT": "Production"
}  

Since launchsettings.json uses ASPNETCORE_ENVIRONMENT as a key for selecting the associated appsettings, I can't use .NET Core's built-in configuration such as RELEASE, STAGING, and DEVELOPMENT. Consequently, my code as well as its dependencies can't respond to the built-in configuration.

Is there a workaround for this?

答案1

得分: 0

appsettings.client_1_ENV.json/appsettings.client_2_ENV.json 中配置 "ASPNETCORE_ENVIRONMENT": "Production" 没有意义,根据这个 文档,它们会被 launchsettings.json 中的值覆盖,你的环境名称会变成 Client_1_ENV/Client_2_ENV

你可以尝试手动添加 JSON 文件进行调试(注意具有相同名称的部分会被覆盖):

launchsettings.json:

"profiles": {
    "Client_1": {
        ...
        "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Development.Client_1"
        }
    },
    "Client_2": {
        ...
        "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Development.Client_2"
        }
    }
}

在 Program.cs 中:

var env = builder.Environment.EnvironmentName;
var realenv = env.Split('.')[0];
var filename = String.Format("appsettings.{0}.json", realenv);
builder.Configuration.AddJsonFile(filename);
var targetstr = builder.Configuration.GetSection("MySection").Value;
var connectstr = builder.Configuration.GetConnectionString("ConStr");
builder.Environment.EnvironmentName = realenv;

在 appsettings.Development.json 中:

"MySection": "Sction1"

如何在不覆盖.NET Core内置配置的情况下启动我的自定义配置?

结果:

如何在不覆盖.NET Core内置配置的情况下启动我的自定义配置?

英文:

It makes no sense to configure "ASPNETCORE_ENVIRONMENT": "Production" in appsettings.client_1_ENV.json/appsettings.client_2_ENV.json

They would be all overrided by that in launchsettings.json accroding to this document, and your enviromentname would be Client_1_ENV/Client_2_ENV

You could try add the jsonfile mannully as below for debug(Note the section share the same name would be overrided):

launchsettings.json:

"profiles": {
    "Client_1": {
      ......
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development.Client_1"
      }
    },
    "Client_2": {
      ......
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development.Client_2"
      }
    },

In Program.cs:

var env = builder.Environment.EnvironmentName;
var realenv = env.Split('.')[0];
var filename = String.Format("appsettings.{0}.json", realenv);
builder.Configuration.AddJsonFile(filename);
var targetstr = builder.Configuration.GetSection("MySection").Value;
var connectstr = builder.Configuration.GetConnectionString("ConStr");
builder.Environment.EnvironmentName = realenv;

in appsettings.Development.json:

"MySection": "Sction1"

如何在不覆盖.NET Core内置配置的情况下启动我的自定义配置?

Result:

如何在不覆盖.NET Core内置配置的情况下启动我的自定义配置?

huangapple
  • 本文由 发表于 2023年5月29日 13:39:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76354904.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定