.net - iis 中的取消标记未触发

标签 .net asp.net-core .net-core iis-express cancellation-token

我有一个带有 .net core 2.1 的 API,并在操作中使用取消 token 。当我使用 iis 运行项目并在浏览器中发出一个简单的请求时,在我取消它之后,它无法按预期工作并运行所有带有传递给它们的取消 token 的方法。 但是当我使用 kestrel 服务器运行项目时,取消请求会导致服务器按预期工作。这是为什么?

    [HttpGet("get")]
    public async Task<IActionResult> GetSome(CancellationToken ct)
    {

        _logger.LogInformation("The slow Request Start");
        try
        {
            await Task.Delay(10_000, ct);
            _logger.LogCritical("Successful");
        }
        catch (Exception)
        {
            _logger.LogInformation("Task Cancelled");
        }
        var message = "The request finished";

        _logger.LogInformation(message);

        return NoContent();
    }

最佳答案

目前,在 IIS 中(或更具体的后面)托管最高 2.1 的 ASP.NET Core 时,这是不可能的,取消或连接丢失不会传递给 ASP.NET Core 应用程序。

目前它仅在直接通过 Kestrel 或 HTTP.SYS(以前称为 Weblistener)托管/公开时有效。

在 ASP.NET Core 2.2(预览版 3 及更高版本)中,AspNetCoreModule 已更新以支持此功能。但是,它仅限于 IIS 进程内托管,并且当 IIS 仅充当反向代理时将不起作用。

参见 ASP.NET Core 2.2-preview3 Announcement blog .

We added support for the ability to detect client disconnects when you’re using the new IIS in-process hosting model. The HttpContext.RequestAborted cancellation token now gets tripped when your client disconnnects.

但是,请注意,虽然进程内可以提高性能并减少开销,但如果我没记错的话,这也意味着每个应用程序池只能将一个应用程序托管为进程内。

关于.net - iis 中的取消标记未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53044815/

相关文章:

c# - EF Core 的 ConstantExpression 中的正确集合

.net-core - 向特定用户发送 SignalR 消息

c# - 编译 C# 不安全代码

docker - 亚马逊弹性容器服务-与ECS等效的Kubernetes事件/就绪检查

c# - "The {0} field is required"消息本地化

c# - 如何从运行 .NET 4.7.2 的 WPF 访问 gRPC 服务?

.net-core - 承载错误 ="invalid_token", error_description ="The signature is invalid"

c# - 加速 Entity Framework 4.2 POCO

c# - 如何发出代码并将其注入(inject)加载的程序集中?

ASP.NET 1.1 到 4.0 迁移 : events not working