C# ASP.NET Web API+;长时间运行操作取消

C# ASP.NET Web API+;长时间运行操作取消,c#,asp.net-mvc-4,asp.net-web-api,C#,Asp.net Mvc 4,Asp.net Web Api,是否有办法在ASP.NET Web API beta中确定HTTP请求是否已取消(由的用户出于任何其他原因中止)?我正在寻找一种开箱即用的取消令牌的机会,它将发出请求中止的信号,因此长时间运行的ops也应该中止 可能的相关问题-CancellationTokenModelBinder类的用例。为什么要为取消令牌使用单独的活页夹?您可以不时检查响应。IsClientConnected以查看浏览器是否仍连接到服务器。我想总结一下。唯一有效的方法是检查Response.IsClientConnecte

是否有办法在ASP.NET Web API beta中确定HTTP请求是否已取消(由的用户出于任何其他原因中止)?我正在寻找一种开箱即用的取消令牌的机会,它将发出请求中止的信号,因此长时间运行的ops也应该中止


可能的相关问题-CancellationTokenModelBinder类的用例。为什么要为取消令牌使用单独的活页夹?

您可以不时检查
响应。IsClientConnected
以查看浏览器是否仍连接到服务器。

我想总结一下。唯一有效的方法是检查Response.IsClientConnected。 以下是有关后台工作的一些技术细节: 及 这种方法有一些缺陷:

  • 仅在IIS下工作(无自托管,无开发服务器)
  • 根据一些人的说法,答案可能很慢(在客户端断开连接后不要立即作出反应):
  • 关于此通话成本,需要考虑以下因素:
最后,我编写了以下代码,将基于IsClientConnected的CancellationToken注入Web API控制器:

    public class ConnectionAbortTokenAttribute : System.Web.Http.Filters.ActionFilterAttribute
{
    private readonly string _paramName;
    private Timer _timer;
    private CancellationTokenSource _tokenSource;
    private CancellationToken _token;

    public ConnectionAbortTokenAttribute(string paramName)
    {
        _paramName = paramName;
    }

    public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext)
    {
        object value;
        if (!actionContext.ActionArguments.TryGetValue(_paramName, out value))
        {
            // no args with defined name found
            base.OnActionExecuting(actionContext);
            return;
        }

        var context = HttpContext.Current;
        if (context == null)
        {
            // consider the self-hosting case (?)
            base.OnActionExecuting(actionContext);
            return;
        }

        _tokenSource = new CancellationTokenSource();
        _token = _tokenSource.Token;
        // inject
        actionContext.ActionArguments[_paramName] = _token;
        // stop timer on client disconnect
        _token.Register(() => _timer.Dispose());

        _timer = new Timer
        (
            state =>
            {
                if (!context.Response.IsClientConnected)
                {
                    _tokenSource.Cancel();
                }
            }, null, 0, 1000    // check each second. Opts: make configurable; increase/decrease.
        );

        base.OnActionExecuting(actionContext);
    }

    /*
     * Is this guaranteed to be called?
     * 
     * 
     */
    public override void OnActionExecuted(System.Web.Http.Filters.HttpActionExecutedContext actionExecutedContext)
    {
        if(_timer != null)
            _timer.Dispose();

        if(_tokenSource != null)
            _tokenSource.Dispose();

        base.OnActionExecuted(actionExecutedContext);
    }
}

如果将CancellationToken添加到控制器方法中,框架将自动注入该令牌,并且当客户端调用xhr.abort()时,该令牌将自动取消

类似于

public Task<string> Get(CancellationToken cancellationToken = default(CancellationToken))
对于.NetCore

services.AddTransient<ICustomInterface>(provider => { 
     var accessor = provider.GetService<IHttpContextAccessor>);
     accessor.HttpContext.RequestAborted;
  }); 
services.AddTransient(提供程序=>{
var accessor=provider.GetService);
accessor.HttpContext.RequestAborted;
}); 

这似乎来自ASP.NET MVC堆栈,而不是ASP.NET MVC Web API。请纠正我的错误。这是从核心ASP.Net引擎。仅适用于IIS集成管道(不适用于VS调试模式)。MSDN包含了一个很好的文档:感谢您的建议。这似乎是唯一的选择,我可能会在此基础上构建解决方案。还有一个选择是使用信号器和类似的解决方案,但它们对我来说有点超出范围。
services.AddTransient<ICustomInterface>(provider => { 
     var accessor = provider.GetService<IHttpContextAccessor>);
     accessor.HttpContext.RequestAborted;
  });