2017-12-16 3 views
0

ASP.NET 코어 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 메서드에서 내가 위치한 환경을 찾는 방법을 알지 못합니다. 아무도이 일을 올바른 방법은 무엇입니까 말해 줄래?

답변