我有一个带有 .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/