Ber53rker
Ber53rker

Reputation: 1038

.NET Core 3 API - Stops working after one request?

I'm having a strange issue where requests only work once. It happens in both IIS and IIS Express. If I restart/recycle, it'll work again, but still just once. Placing a breakpoint reveals subsequent requests never hit the controller...

[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());
 }

Any suggestions would be greatly appreciated! Thanks!

Upvotes: 3

Views: 1371

Answers (2)

user11709744
user11709744

Reputation:

In my case, my Core 3.0 API shut down after I sent a request to it because I had an infinite loop in my repositories, like this:

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);
    }
}

So, in EstabelecimentoRepository I took out the empresaRepository and my API went back to work.

Upvotes: 0

Ber53rker
Ber53rker

Reputation: 1038

Turns out it was because I was setting the HttpContext to a field.

There's a bunch of do's and don'ts, which can be found here: https://github.com/davidfowl/AspNetCoreDiagnosticScenarios/blob/master/AspNetCoreGuidance.md#do-not-store-ihttpcontextaccessorhttpcontext-in-a-field

Upvotes: 0

Related Questions