首页 > 解决方案 > ASPNETCORE_ENVIRONMENT 设置为“开发”以某种方式限制 API 调用

问题描述

我正在研究一个简单的 .net 核心 API,并注意到一个我无法解释的奇怪行为:

API 工作正常,直到每次调用失败并返回 HTTP 错误 502.3 - 超时后网关错误的点(40-50 次调用)。

经过一些调查,我发现当我删除配置文件中的 ASPNETCORE_ENVIRONMENT (设置为开发)时,这种行为没有出现。

那么,具体来说,哪些差异意味着我的 ASPNETCORE_ENVIRONMENT 设置为 Development?我怎样才能让它与这个设置一起工作?

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)
    {
        var dbString = Configuration["Database:ConnectionString"];

        services.AddScoped<ISearchEngineRepository, SearchEngineRepository>();
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        services.AddDbContext<VvpDbContext>(options => options.UseSqlServer(dbString), ServiceLifetime.Transient);
        services.AddSingleton(Configuration);
        services.AddCors();
    }

    // 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();
            app.UseCors(options => options.WithOrigins("http://localhost:4200").AllowAnyMethod());
        }
        else
        {
            app.UseHsts();
        }

        app.UseMvc();
    }
}

标签: .netiisasp.net-coreasp.net-core-webapi

解决方案


推荐阅读