Stop Kestrel server accepting requests from code

Viewed 464

I am trying to accomplish the following: I have a asp.net core 3.1 running a gRPC service.

During the lifetime of the application I want to be able to Pause/Stop the endpoint/Server from listening/accepting requests and after a while resuming it.

Just to clarify I don't want to stop the process

I couldn't find any Kestrel commands relating to it, any help will be appreciated

2 Answers

During the lifetime of the application I want to be able to Pause/Stop the endpoint/Server from listening/accepting requests and after a while resuming it.

This isn't possible today but will be in 5.0. I'd suggest commenting on this issue https://github.com/dotnet/aspnetcore/issues/21244

In Startup Configure(IApplicationBuilder app, IWebHostEnvironment env, ISomeDep someDep) You can add at the start of the method following lines:

app.Use((context, func) =>
        {
            if (someDep.IsReadyToAccept) 
                return func.Invoke();
            context.Response.StatusCode = 204;//if u want to return specific status code when not ready to accept requests
            return Task.CompletedTask;
        });
Related