I have ASP.Net Core 3 application with Entity Framework. Database credentials are in AWS Systems Manager. AWS is not important; what is important that credentials are retrieved at runtime). In Core 2 I was injecting AWS service in Program.cs
, but in Core 3 it is no longer possible.
This is relevant part of what I have (for clarification, IAmazonSimpleSystemsManagement
is AWS service, and SSMService
is my service that retrieves the credentials:
public void ConfigureServices(IServiceCollection services) {
services.AddAWSService<IAmazonSimpleSystemsManagement>();
services.AddSingleton<ISSMService, SSMService>();
services.AddDbContext<MyContext>();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env,
IOptions<DbContextOptionsBuilder<MyContext>> options, ISSMService ssmService) {
string path = Configuration.GetValue<string>("SystemsManager:ParametersPath");
Dictionary<string, string> parameters = ssmService.GetParametersAsync().Result;
SqlConnectionStringBuilder builder =
new SqlConnectionStringBuilder(Configuration.GetConnectionString("DefaultConnection"))
{
UserID = parameters[$"{path}/DbUser"], Password = parameters[$"{path}/DbPassword"]
};
options.Value.UseSqlServer(builder.ConnectionString);
}
I tried other types of IOptions, like non-generic DbContextOptionsBuilder
or DbContextOptions
. In all cases I get an exception at runtime that No database provider has been configured for this DbContext.
It seems that options that I am setting up in Configure are not applied to the service that I am registering in ConfigureServices. How do I tie them together? I can pass MyContext
- maybe there is a way to set connection string that way?