简体   繁体   English

避免在ASP.NET Core Program.cs中使用静态值

[英]Avoid static value in ASP.NET Core Program.cs

In my CreateWebHostBuilder() method I've added the AWS Systems Manager Parameter Store as an additional source for Configuration Builder: 在我的CreateWebHostBuilder()方法中,我添加了AWS Systems Manager参数存储作为Configuration Builder的其他来源:

public class Program
{
    public static void Main(string[] args)
    {
        CreateWebHostBuilder(args).Build().Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args)
    {
        return WebHost.CreateDefaultBuilder(args)
                  .ConfigureAppConfiguration(builder =>
                  {
                      builder.AddSystemsManager("/ConfigureStoreName/");
                  })
                  .UseStartup<Startup>();
    }
}

Instead of hardcoding "/ConfigureStoreName/ " I'd like to make this a configuration value. 而不是硬编码“ / ConfigureStoreName / ”,我想将此配置值。

When I call .ConfigureAppConfiguration() do I have access to the config values from appsettings.json that .CreateDefaultBuilder() uses? 当我打电话.ConfigureAppConfiguration()我有从appsettings.json访问配置值.CreateDefaultBuilder()使用? If so how would I update my code to call it? 如果是这样,我将如何更新代码来调用它? If not what is the best approach to avoid using a static value in the CreateWebHostBuilder() method? 如果不是,最好的方法是避免在CreateWebHostBuilder()方法中使用静态值?

Pre load setting file with that information. 带有该信息的预加载设置文件。

If for example the setting file contained 例如,如果包含设置文件

{
  //...

  "AWS": {
    "Profile": "local-test-profile",
    "Region": "us-west-2",
    "ConfigureSource": {
      "Path": "/ConfigureStoreName/"
    }
  }

  //...
}

load that up in a configuration to extract the value. 将其加载到配置中以提取值。

public static IWebHostBuilder CreateWebHostBuilder(string[] args) {

    var configuration = new ConfigurationBuilder()
        .AddJsonFile("appsettings.json") //<-- or whichever file has that information
        .Build();

    string path = configuration.GetValue<string>("AWS:ConfigureSource:Path");
    //Or a strongly typed model with aws options

    return WebHost.CreateDefaultBuilder(args)
              .ConfigureAppConfiguration(builder =>
              {
                  builder.AddSystemsManager(path);
              })
              .UseStartup<Startup>();
}

The solution I ended up going with was to use an Environment Variable: 我最终使用的解决方案是使用环境变量:

public class Program
{
    private static string _parameterStoreNamePath;

    public static void Main(string[] args)
    {
        _parameterStoreNamePath = Environment.GetEnvironmentVariable("AWS_PARAMETER_STORE_NAME");
        CreateWebHostBuilder(args).Build().Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args)
    {
        return WebHost.CreateDefaultBuilder(args)
                  .ConfigureAppConfiguration(builder =>
                  {
                      builder.AddSystemsManager(_parameterStoreNamePath); 
                  })
                  .UseStartup<Startup>();
    }
}

Notes: 笔记:

  1. In the Visual Studio project, create an Environmental Variable named AWS_PARAMETER_STORE_NAME. 在Visual Studio项目中,创建一个名为AWS_PARAMETER_STORE_NAME的环境变量。
  2. For deployed instances, AWS_PARAMETER_STORE_NAME needs to be set in serverless.template or directly added to Lambda via the console. 对于已部署的实例,需要在serverless.template中设置AWS_PARAMETER_STORE_NAME或通过控制台将其直接添加到Lambda。

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

相关问题 ASP.NET 核心程序.cs配置 - ASP.NET Core program.cs configuration 如何返回在 Program.cs ASP.Net core 6 中找不到 - How to return not found in Program.cs ASP.Net core 6 ASP.NET Core 6 MVC:如何从 controller 访问 Program.cs 中定义的变量值? - ASP.NET Core 6 MVC : how to access a variable's value that is defined in Program.cs from a controller? ASP.NET 核心 Web API - 如何将 .NET 核心 5 中的 SetupSerilog 转换为 .NET 核心 6 Program.cs - ASP.NET Core Web API - How to convert SetupSerilog in .NET Core 5 to .NET Core 6 Program.cs 只要ASP.NET Core中有可用的program.cs / startup.cs,即可获取当前URL - Get Current URL as soon as available program.cs / startup.cs in ASP.NET Core ASP.NET 内核在 Program.cs 和 Startup.cs 中捕获并显示错误 - ASP.NET Core catch and display error inside Program.cs and Startup.cs ASP.NET Core 2 中的 Startup.cs 与 Program.cs - Startup.cs vs Program.cs in ASP.NET Core 2 在 ASP.NET Core 6 中的 Program.cs 中添加命名空间和 Main 方法会有什么负面影响吗? - Will adding a namespace and a Main method to Program.cs in ASP.NET Core 6 have any negative effects? 如何在 asp.net mvc 核心的 program.cs 中添加 pgadmin db - How to add pgadmin db in program.cs in asp.net mvc core 在ASP.NET Core中,有没有办法从Program.cs设置中间件? - in ASP.NET Core, is there any way to set up middleware from Program.cs?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM