.NET Core 3 API - 在一个请求后停止工作?
.NET Core 3 API - Stops working after one request?
我遇到了一个奇怪的问题,请求只能工作一次。它发生在 IIS 和 IIS Express 中。如果我 restart/recycle,它会再次工作,但仍然只有一次。放置断点显示后续请求永远不会命中控制器...
[HttpGet("private/{zipCode}")]
public IActionResult PrivateZipSearch(string zipCode)
Startup.Configure
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());
}
如有任何建议,我们将不胜感激!谢谢!
原来是因为我将 HttpContext 设置为一个字段。
有很多注意事项,可以在这里找到:
https://github.com/davidfowl/AspNetCoreDiagnosticScenarios/blob/master/AspNetCoreGuidance.md#do-not-store-ihttpcontextaccessorhttpcontext-in-a-field
在我的例子中,我的 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 回去工作了。
我遇到了一个奇怪的问题,请求只能工作一次。它发生在 IIS 和 IIS Express 中。如果我 restart/recycle,它会再次工作,但仍然只有一次。放置断点显示后续请求永远不会命中控制器...
[HttpGet("private/{zipCode}")]
public IActionResult PrivateZipSearch(string zipCode)
Startup.Configure
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());
}
如有任何建议,我们将不胜感激!谢谢!
原来是因为我将 HttpContext 设置为一个字段。
有很多注意事项,可以在这里找到: https://github.com/davidfowl/AspNetCoreDiagnosticScenarios/blob/master/AspNetCoreGuidance.md#do-not-store-ihttpcontextaccessorhttpcontext-in-a-field
在我的例子中,我的 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 回去工作了。