0
私はASP.NET Core 2アプリケーションを構築しています。アプリケーションをHerokuにデプロイしたいのですが、環境変数$ DATABASE_URLから接続文字列をロードする必要があります。私のstartup.cs
では私が持っている:ASP.NETコアロード接続文字列
namespace LearningSystems
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
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)
{
services.AddMvc().AddJsonOptions(options =>
{
options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
});
services.AddDistributedMemoryCache();
services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromMinutes(20);
options.Cookie.HttpOnly = true;
});
SetupDbContext(services);
}
private void SetupDbContext(IServiceCollection services)
{
var conn = Environment.GetEnvironmentVariable("$DATABASE_URL");
var connectionString = Configuration.GetConnectionString("pmf");
services.AddEntityFrameworkNpgsql()
.AddDbContext<pmf_visualizationsContext>(options => options.UseNpgsql(connectionString));
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseSession();
app.UseStaticFiles();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "Home",
template: "",
defaults: new {controller = "Shell", action = "Index"}
);
});
}
}
}
私は本番環境(Herokuの)で午前ときに別の接続文字列をロードします。 しかし、SetupDbContextメソッドでは、私がどの環境にいるのかを知る方法がわかりません。誰かがこれを行う正しい方法は何か教えてもらえますか?