.NET 核心 AWS RDS 连接

.NET Core AWS RDS CONNECTION

我正在亚马逊 AWS Elastic Beanstalk 上创建 .NET Core Web API。 我正在尝试添加数据库,但他们的添加数据库指南不适用于 .Net Core http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_NET.rds.html

上面说要使用"ConfigurationManager.AppSettings;"获取相关信息,但是在.NET Core中这是不可能的。

任何人都可以提供一些有关如何获取数据库信息的信息吗? ( "RDS_DB_NAME" "RDS_USERNAME" "RDS_PASSWORD" "RDS_HOSTNAME" )

更新 我试着阅读 https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration 但是我还没有解决问题。我似乎仍然无法从 AWS 获取值。

它只是 returns 我自己设置的 appsettings.json 这是我的代码: MyOptions.cs

public class MyOptions
{
    public MyOptions()
    {
        // Set default value.
    }
    public string RDS_HOSTNAME { get; set; }
    public string RDS_PORT { get; set; }
    public string RDS_DB_NAME { get; set; }
    public string RDS_USERNAME { get; set; }
    public string RDS_PASSWORD { get; set; }
}

StartUp.cs

public void ConfigureServices(IServiceCollection services)
{
    // Register the IConfiguration instance which MyOptions binds against.
    services.Configure<MyOptions>(Configuration);
    // Add framework services.
    services.AddApplicationInsightsTelemetry(Configuration);

    services.AddMvc();
}

HomeController.cs

namespace WebApplication2.Controllers
{
    [Route("")]
    public class HomeController : Controller
    {
        private readonly MyOptions _options;

        public HomeController(IOptions<MyOptions> optionsAccessor)
        {
            _options = optionsAccessor.Value;
        }

        [HttpGet]
        public IActionResult Index()
        {
            var RDS_DB_NAME = _options.RDS_DB_NAME;
            var RDS_HOSTNAME = _options.RDS_HOSTNAME;
            var RDS_PASSWORD = _options.RDS_PASSWORD;
            var RDS_PORT = _options.RDS_PORT;
            var RDS_USERNAME = _options.RDS_USERNAME;
            return Content($"RDS_DB_NAME = {RDS_DB_NAME}, RDS_HOSTNAME = {RDS_HOSTNAME}, RDS_PASSWORD = { RDS_PASSWORD}, RDS_PORT = {RDS_PORT}, RDS_USERNAME = { RDS_USERNAME}");
        }
    }
}

我 运行 进入了这个确切的问题,它比我预期的要复杂得多。

第 1 步 - 我修改了另一个 Stack Overflow 问题的 。我在 Startup.cs 中的代码如下所示:

public Startup(IHostingEnvironment env)
{
    var builder = new ConfigurationBuilder()
        .SetBasePath(env.ContentRootPath)
        .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
        .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true)
        .AddJsonFile(@"C:\Program Files\Amazon\ElasticBeanstalk\config\containerconfiguration", optional: true, reloadOnChange: true)
        .AddEnvironmentVariables();

    // This adds EB environment variables.
    builder.AddInMemoryCollection(GetAwsDbConfig(builder.Build()));

    Configuration = builder.Build();
}

private Dictionary<string, string> GetAwsDbConfig(IConfiguration configuration)
    {
        Dictionary<string, string> dict = new Dictionary<string, string>();

        foreach (IConfigurationSection pair in configuration.GetSection("iis:env").GetChildren())
        {
            string[] keypair = pair.Value.Split(new[] { '=' }, 2);
            dict.Add(keypair[0], keypair[1]);
        }

        return dict;
    }

private string GetRdsConnectionString()
{
    string hostname = Configuration.GetValue<string>("RDS_HOSTNAME");
    string port = Configuration.GetValue<string>("RDS_PORT");
    string dbname = Configuration.GetValue<string>("RDS_DB_NAME");
    string username = Configuration.GetValue<string>("RDS_USERNAME");
    string password = Configuration.GetValue<string>("RDS_PASSWORD");

    return $"Data Source={hostname},{port};Initial Catalog={dbname};User ID={username};Password={password};";
}

步骤 2 - 您需要转到 AWS 控制台中的 RDS 服务。 Select 您要连接的实例 -> 实例操作 -> 查看详细信息。您将能够找到 RDS_HOSTNAME(端点)和 RDS_PORT(端口)。

RDS_DB_NAME 是您的代码要使用的数据库名称。

RDS_USERNAME 和 RDS_PASSWORD 是您在 Elastic Beanstalk 中创建数据库时使用的主用户名和密码。如果您转到 Elastic Beanstalk -> 配置 -> 数据层 ->(单击齿轮),您会看到您的主用户名,如果您忘记了密码,可以选择更改密码。

第 3 步 - 现在您已拥有所有数据,您必须在 Elastic Beanstalk 中输入这些选项作为环境属性。转到 Elastic Beanstalk -> 软件配置 ->(单击齿轮)。此页面底部是环境属性。您需要在此处输入第一步中代码中的名称和第二步中 RDS 中的值。

有关来自 AWS 文档的更多信息,请查看 here and here