Asp.net core ServiceFabric解决方案中的核心3.1 API,未读取本地机密
我现在正忙得不可开交。我已经尝试了多种不同的方法来获取这些秘密。以下是我的工作:Asp.net core ServiceFabric解决方案中的核心3.1 API,未读取本地机密,asp.net-core,.net-core,secretsmanager,Asp.net Core,.net Core,Secretsmanager,我现在正忙得不可开交。我已经尝试了多种不同的方法来获取这些秘密。以下是我的工作: public class Startup { public Startup(IConfiguration configuration, IWebHostEnvironment env) { //Configuration = configuration; // <<=== THIS DOES NOT WORK - CONFIG IS ALWAYS EMPTY ===
public class Startup
{
public Startup(IConfiguration configuration, IWebHostEnvironment env)
{
//Configuration = configuration; // <<=== THIS DOES NOT WORK - CONFIG IS ALWAYS EMPTY ===
// Manually add configuration...
var app = Assembly.Load(new AssemblyName(env.ApplicationName));
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true) // When app is published
.AddEnvironmentVariables();
if (app != null && env.IsDevelopment())
builder.AddUserSecrets(app, optional: false);
Configuration = builder.Build();
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
var connectionString = Configuration.GetConnectionString("MySqlConn");
var password = Configuration["MyPassword"]; // <<=== THIS FAILS - ALWAYS NULL ===
var builder = new SqlConnectionStringBuilder(connectionString);
builder.Password = password;
connectionString = builder.ConnectionString;
services.AddDbContext<MySqlContext>(options => options.UseSqlServer(connectionString));
services.AddControllers();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseSwagger();
app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API v1"); c.RoutePrefix = string.Empty; });
if (!env.IsProduction())
app.UseDeveloperExceptionPage();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
}
}
公共类启动
{
公共启动(IConfiguration配置,IWebHostEnvironment环境)
{
//Configuration=Configuration;//服务结构在本地运行时,在Docker容器中运行。这意味着容器中运行的任何东西都无法访问Secrets.json文件本机所在的文件夹。短期内(hack),我将Secrets.json移到了我的应用程序根文件夹(使用appsettings.json)并在my.gitignore中将其标记,以便不签入。我还将其添加到上述配置代码中。工作正常,但默认配置永远不会在那里找到它,因此我保留我的手动配置生成器。Hi@Keith,它可以与开发环境中的设置configuration=configuration;
配合使用。Cou您是否将日志设置为跟踪级别,以检查在使用Configuration=Configuration;
时是否存在异常?您使用的环境是什么?开发还是生产?从手动添加配置中,您无法添加secrets.json
文件。托管SF local是在Docker容器中完成的。无需进入映射要在Docker内部访问Secrets,则无法访问Secrets位置。我将Secrets.json移动到我的应用程序根目录,并将其标记为.gitignore.A hack,但它是works.Env=Dev,本地计算机。