简体   繁体   English

多个环境.NET Core

[英]Multiple environments .NET Core

I have a .NET Core project and I need to manage multiple environments. 我有一个.NET Core项目,我需要管理多个环境。 Let's say I have Development, Staging and Production environment. 假设我有开发,分期和生产环境。 I have to manage different variables for each environment. 我必须为每个环境管理不同的变量。 In my Startup.cs I have already managed to have appsettings.{env.EnvironmentName}.json working, so that when i start my application with --launch-profile "ProfileName" I have the right configuration. 在我的Startup.cs我已经设法有appsettings.{env.EnvironmentName}.json工作,所以当我用--launch-profile "ProfileName"启动我的应用程序时,我有正确的配置。

Now my problem is that I have to deploy this solution, so I have to do something like dotnet publish --launch-profile "ProfileName" ; 现在我的问题是我必须部署这个解决方案,所以我必须做一些像dotnet publish --launch-profile "ProfileName" ; i know that this syntax doesn't exist , that's why I am asking this question. 我知道这个语法不存在 ,这就是我问这个问题的原因。 I can't set the ASPNETCORE_ENVIRONMENT variable machine wide on the host , since I have multiple deploys (with multiple environments) on the same machine. 我无法在主机上设置ASPNETCORE_ENVIRONMENT变量机器 ,因为我在同一台机器上有多个部署(具有多个环境)。 I need a way to deploy with the right profile, so that the right appsettings.json is recognized by the right application. 我需要一种使用正确的配置文件进行部署的方法,以便正确的应用程序识别正确的appsettings.json Thank you. 谢谢。

No, dotnet publish does not provide possibility to deploy specific version of configuration file for specified environment. 不, dotnet publish不提供为指定环境部署特定版本的配置文件的可能性。 And I believe it's good because follows separation of concerns principle. 而且我认为这很好,因为关注分离原则。

You still have several options for covering your use case: 您仍有几个选项可以涵盖您的用例:

1.Powershell allows setting environment variable only for current PowerShell session with $Env command. 1.Powershell允许仅使用$Env命令为当前PowerShell会话设置环境变量。 You could create simple powershell launcher that sets ASPNETCORE_ENVIRONMENT environment variable and then launches the application: 您可以创建简单的ASPNETCORE_ENVIRONMENT启动程序来设置ASPNETCORE_ENVIRONMENT环境变量,然后启动应用程序:

$Env:ASPNETCORE_ENVIRONMENT = "Production"
# Command for launching your application

ASPNETCORE_ENVIRONMENT environment variable will be accessible only within current session and will not conflict will other deployments of your application. ASPNETCORE_ENVIRONMENT环境变量只能在当前会话中访问,并且不会与应用程序的其他部署发生冲突。

This solution is somehow similar to approach suggested by Tomas Lycken, but does not require creation of service account for each environment. 此解决方案与Tomas Lycken建议的方法类似,但不需要为每个环境创建服务帐户。

2.You could also shift away from dynamic selection of configuration file ( appsettings.{env.EnvironmentName}.json ). 2.您也可以从动态选择配置文件( appsettings.{env.EnvironmentName}.jsonappsettings.{env.EnvironmentName}.json You could put all environment-specific settings to one file (eg appSettings.environemntSpecific.json ) and deploy only one version required for some particular environment (eg Development or Production). 您可以将所有特定于环境的设置放在一个文件中(例如appSettings.environemntSpecific.json ),并仅部署某个特定环境(例如开发或生产)所需的一个版本。 In this case you don't need setting ASPNETCORE_ENVIRONMENT at all. 在这种情况下,您根本不需要设置ASPNETCORE_ENVIRONMENT

To do this, load new configuration file from BuildWebHost method: 为此,请从BuildWebHost方法加载新配置文件:

public static IWebHost BuildWebHost(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
        .ConfigureAppConfiguration((hostingContext, config) =>
        {
            config.AddJsonFile("appSettings.environemntSpecific.json", optional: true);
        }).UseStartup<Startup>()
        .Build();

The final step here is to put required version of appSettings.environemntSpecific.json file after dotnet publish command. 这里的最后一步是在dotnet publish命令之后放置所需版本的appSettings.environemntSpecific.json文件。 Again, dotnet publish does not provide any possibility for such selection, so you should do it by yourself with simplest copy command. 同样, dotnet publish不提供任何此类选择的可能性,因此您应该使用最简单的copy命令自行完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM