停止 Kestrel 服务器接受来自代码的请求

Stop Kestrel server accepting requests from code

我正在尝试完成以下任务: 我有一个 asp.net core 3.1 运行 gRPC 服务。

在应用程序的生命周期中,我希望能够 Pause/Stop 来自 listening/accepting 请求的 endpoint/Server 并在一段时间后恢复它。

澄清一下,我不想停止进程

我找不到任何与之相关的 Kestrel 命令,我们将不胜感激

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.

这在今天是不可能的,但会在 5.0 中实现。我建议对这个问题发表评论 https://github.com/dotnet/aspnetcore/issues/21244

正在启动中Configure(IApplicationBuilder app, IWebHostEnvironment env, ISomeDep someDep) 您可以在方法的开头添加以下行:

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