【问题标题】:ASP.NET Core ignores ASPNET_ENV and Hosting:EnvironmentASP.NET Core 忽略 ASPNET_ENV 和 Hosting:Environment
【发布时间】:2017-08-20 03:57:18
【问题描述】:

无论我何时何地设置ASPNET_ENVHosting:Environment,启动代码总是会输入

//This method is invoked when ASPNET_ENV is 'Production'
//The allowed values are Development,Staging and Production
public void ConfigureProduction(IApplicationBuilder app, ILoggerFactory loggerFactory)
{
   loggerFactory.AddConsole(minLevel: LogLevel.Warning);

   Configure(app);
}

到目前为止我已经尝试过什么

  • 在项目属性中将Hosting:Environment设置为Development
  • 在项目属性中将ASPNET_ENV设置为Development
  • launchSettings.json 中将Hosting:Environment 设置为Development
  • launchSettings.json 中将ASPNET_ENV 设置为Development
  • 在调用ConfigurationBuilder.GetEnvironmentVariables()之前,在Startup方法中通过Environment.SetEnvironmentVariable("ASPNET_ENV", "Development");在代码中将ASPNET_ENV设置为Development

顺便说一下,这是版本1.0.0-rc2-20143。我在这里遗漏了什么还是只是一个错误?

【问题讨论】:

    标签: asp.net-core asp.net-core-mvc asp.net-core-1.0


    【解决方案1】:

    环境变量名称已更改为 ASPNETCORE_ENVIRONMENT 作为名称更改的一部分。

    this issue 公布,在this PR 更改。

    【讨论】:

    • 谢谢,确实是这个问题。
    • 我还必须实际设置ASPNET_ENVIRONMENT,不知道这里发生了什么。
    • 原来编辑路径变量会改变launchSettings.json,但反过来不会。确保只在项目属性中设置ASPNETCORE_ENVIRONMENT
    【解决方案2】:

    如果您还没有这样做,请尝试将环境变量添加到配置实例中。

    public static void Main(string[] args)
    {
        var builder = new ConfigurationBuilder();
        builder.AddJsonFile("appsettings.json");
        builder.AddEnvironmentVariables();
        var config = builder.Build();
    }
    

    【讨论】:

    • 做到了,没有区别。控制台窗口中的 Kestrel 输出一直声称 Hosting environment: Production。我倾向于说这确实是一个错误。
    【解决方案3】:

    虽然 Henk Mollema 的回答对于环境变量名称是正确的,但我仍然遇到 dotnetcore 似乎会忽略环境变量并在从命令行运行 时使用 appsettings.json 的问题

    为确保它使用正确的环境和应用设置,请尝试以下操作:

    1. 通过Project Properties -> Debug -> Environment Variables -> ASPNETCORE_ENVIRONMENT -> Value 更改环境变量:更改值以匹配您的预期环境,例如暂存
    2. Build Solution
    3. 在命令行中,键入 SETX ASPNETCORE_ENVIRONMENT "YOUR-ENVIRONMENT-NAME",其中替换“YOUR-ENVIRONMENT-NAME”以匹配您在第 1 步中设置的内容,例如暂存并确保包含“引号”
    4. 重要 - 确保您所有的各种 appsettings.json 例如 appsettings.staging.json 都存在于您将运行它的项目目录中。 (In the case of a published solution, the appsettings.staging.json may not have been copied, so ensure it's there)
    5. 从命令行转到您的项目目录(或发布的目录)并通过键入dotnet run(或“dotnet YOURPROJECTNAME.DLL”对于已发布的项目)运行您的项目
    6. 观察命令行中显示Hosting environment: YOURENVIRONMENTNAME的下一行,例如Hosting environment:staging

    从 Visual Studio 运行 DotNetCore 项目总是选择 根据项目中设置的环境正确的 appsettings 属性,但是这些是我从命令行正确运行 dotnet core 所遵循的步骤 工作正常。

    【讨论】:

    • 您的意思是launchSettings.json,而不是appsettings.json,对吗?通常 appsettings.json 直到环境确定后才会加载。无论如何,如果您查看 Asp.Net Core 2.0 源代码,只有一种方法可以更改内置的 IHostingEnvironment 的 Environment 属性。那是通过 ASPNETCORE_ENVIRONMENT 环境变量。这是因为 Microsoft.AspNetCore.Hosting.Internal.HostingEnvironment 对象是在 WebHostBuilder.Build() 期间设置的,但在 ConfigureAppConfig() 或 ConfigureServices() 委托/lambda 运行之前。
    • (facepalm) 我的错;我完全间隔实际上有 2 个:您也可以在调用 .Build() 之前使用 WebHostBuilder.UseEnvironment() 扩展方法来更改它。
    【解决方案4】:

    这对我使用 .net core 2.0 和构建 web api 很有效。请注意访问环境变量的 2 种不同方法。

    程序.cs

    public static void Main(string[] args)
        {
    
            BuildWebHost(args).Run();
        }
    
        public static IWebHost BuildWebHost(string[] args){
    
            if(Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT ") == "Production"){
                return WebHost.CreateDefaultBuilder(args)
                 .UseKestrel()
                 .UseContentRoot(Directory.GetCurrentDirectory())
                 .UseIISIntegration()
                 .UseStartup<Startup>()
                 .Build();
            } else {
                return WebHost.CreateDefaultBuilder(args)
                 .UseStartup<Startup>()
                 .Build();
            }
        }
    

    但在启动文件中还有另一个环境选项

    Startup.cs

    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.AddCors(options => options.AddPolicy("AllowAll", p => p.AllowAnyOrigin()
                                                                    .AllowAnyMethod()
                                                                     .AllowAnyHeader()));
            var connectionString = "Data Source=tcp:<some-ip>,<some-port>;Initial Catalog=<some database>;Integrated Security=False;User Id=SA;Password=<some password>;MultipleActiveResultSets=True";
            services.AddDbContext<myContext>(opt => opt.UseSqlServer(connectionString));
            services.AddMvc();
        }
    
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            app.UseCors("AllowAll");
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
    
            app.UseMvc();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-12
      • 2019-08-13
      • 2018-08-07
      • 2018-05-19
      • 2020-07-09
      • 1970-01-01
      • 1970-01-01
      • 2020-01-19
      相关资源
      最近更新 更多