.NET核心AWS RDS连接
我正在Amazons AWS Elastic Beanstalk上创建.NET Core Web API。 我试图添加数据库,但他们的向导添加数据库没有对.NET的核心工作 http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_NET.rds.html.NET核心AWS RDS连接
它说,使用来获得相关信息“ConfigurationManager.AppSettings;”,但这是不可能的.NET核心。
任何人都可以提供一些关于如何获取数据库信息的信息吗? ( “RDS_DB_NAME” “RDS_USERNAME” “RDS_PASSWORD” “RDS_HOSTNAME” )
UPDATE 我试图在https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration 阅读,但我还没有解决的问题。我似乎仍然无法从AWS获取这些值。
它只是返回无论我在我自己的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步 - 我修改了this answer从另一个堆栈溢出问题。我在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};";
}
第二步 - 你需要去RDS服务在AWS控制台。选择您要连接的实例 - >实例操作 - >查看详细信息。您将能够找到RDS_HOSTNAME(端点)和RDS_PORT(端口)。
RDS_DB_NAME是您的代码旨在使用的数据库名称。
RDS_USERNAME和RDS_PASSWORD是您在Elastic Beanstalk中创建数据库时使用的主用户名和密码。如果你转到Elastic Beanstalk - > Configuration - > Data Tier - >(点击Gear),你会看到你的主用户名,如果你忘记了密码,可以选择更改你的密码。
第3步 - 现在您已拥有所有数据,您必须在Elastic Beanstalk中输入这些选项作为环境属性。转至Elastic Beanstalk - >软件配置 - >(点击齿轮)。在此页面的底部是环境属性。这里是您希望在步骤一中输入代码中的名称以及步骤二中的RDS中的值。
您应该查看dotnet核心中的配置管理,网址为https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration –
谢谢Babak。我读了它,但我无法弄清楚如何让它起作用。如果您有时间帮助更多,我现在已经在OP中编写了我的代码。否则,谢谢。 – thatname