【问题标题】:.NET Core 3 API - Stops working after one request?.NET Core 3 API - 一个请求后停止工作?
【发布时间】:2020-02-04 23:15:56
【问题描述】:

我遇到了一个奇怪的问题,请求只能工作一次。它发生在 IIS 和 IIS Express 中。如果我重新启动/回收,它会再次工作,但仍然只有一次。放置断点表明后续请求永远不会命中控制器...

[HttpGet("private/{zipCode}")]
public IActionResult PrivateZipSearch(string zipCode)

启动.配置

public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IServiceProvider srv, ILoggerFactory loggerFactory)
{
     loggerFactory.AddProvider(new DbLoggerProvider(this.Configuration, srv.GetService<IHttpContextAccessor>()));

     app.UseHttpsRedirection(); // Redirect HTTP -> HTTPS
     app.UseRouting();
     {
         app.UseCors(Config.Settings.Cors.PolicyName);
         app.UseAuthentication();
     }

     // Hangfire
     {
         app.UseHangfireDashboard();
         app.UseHangfireServer();
     }

     app.UseMiddleware<LoggingMiddleware>();
     app.UseMiddleware<OptionsVerbMiddleware>();

     // Must be last!
     app.UseEndpoints(x => x.MapControllers());
 }

任何建议将不胜感激!谢谢!

【问题讨论】:

  • 在 IIS 应用程序中停止工作?第一个和第二个请求的响应代码是什么?日志里有什么线索吗?
  • 感谢您的评论@RuardvanElburg。 IIS 没有停止,响应代码都是 200。日志没有提供任何有用的信息。
  • 那我不明白你说的不工作是什么意思。你能再解释一下吗?如果请求返回 200,那么什么不起作用?
  • 第一个请求到达控制器并返回数据,尽管显然返回 200,但后续请求没有。
  • 这表明管道在到达控制器之前发生短路。关于授权的两个请求之间有区别吗?比你可以排除的。从您的代码中有四个潜在的中间件,最有可能是:HangfireDashboard、HangfireServer 和 LoggingMiddleware、OptionsVerbMiddleware。添加日志以查看哪些中间件短路或尝试禁用/启用中间件。并查看过滤器。

标签: asp.net-core-webapi asp.net-core-3.0


【解决方案1】:

原来是因为我将 HttpContext 设置为一个字段。

有很多注意事项,可以在这里找到: https://github.com/davidfowl/AspNetCoreDiagnosticScenarios/blob/master/AspNetCoreGuidance.md#do-not-store-ihttpcontextaccessorhttpcontext-in-a-field

【讨论】:

    【解决方案2】:

    在我的情况下,我的 Core 3.0 API 在我向它发送请求后关闭,因为我的存储库中有一个无限循环,如下所示:

    EmpresaRepository:

        public class EmpresaRepository : BaseRepository<Empresa>, IEmpresaRepository
        {
            private readonly EstabelecimentoRepository estabelecimentoRepository;
    
            public EmpresaRepository(Context Context) : base(Context)
            {
                estabelecimentoRepository = new EstabelecimentoRepository(Context);
            }
        }
    

    EstabelecimentoRepository:

    public class EstabelecimentoRepository : BaseRepository<Estabelecimento>, IEstabelecimentoRepository
    {
        private readonly EmpresaRepository empresaRepository;
    
        public EstabelecimentoRepository(Context Context) : base(Context)
        {
             empresaRepository = new EmpresaRepository(Context);
        }
    }
    

    所以,在EstabelecimentoRepository 中,我取出了empresaRepository,我的 API 又恢复了工作。

    【讨论】:

      猜你喜欢
      • 2013-02-26
      • 2013-10-04
      • 1970-01-01
      • 2020-10-27
      • 1970-01-01
      • 1970-01-01
      • 2013-03-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多