我们是否应该使用的CancellationToken与MVC /网页API控制器? [英] Should we use CancellationToken with MVC/Web API controllers?

查看:323
本文介绍了我们是否应该使用的CancellationToken与MVC /网页API控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有用于异步控制器不同的例子。他们中的一些方法,在定义中使用的CancellationToken:

There are different examples for async controllers. Some of them use CancellationToken in method definition:

public async Task<ActionResult> ShowItem(int id, CancellationToken cancellationToken)
{
    await Database.GetItem(id, cancellationToken);
    ...

但其他的例子,甚至默认的ASP.NET项目VS2013在所有的工作,没有它不使用的CancellationToken:

But other examples and even the default ASP.NET projects for VS2013 don't use CancellationToken at all and work without it:

public async Task<ActionResult> ShowItem(int id)
{
    await Database.GetItem(id);
    ...

目前还不清楚,如果我们应控制器使用的CancellationToken与否(为什么)。

It's not clear, if we should use CancellationToken in controllers or not (and why).

推荐答案

您应该使用它。现在它仅适用<一个href=\"http://www.asp.net/mvc/tutorials/mvc-4/using-asynchronous-methods-in-aspnet-mvc-4#CancelToken\">if你有一个 AsyncTimeout ,但它很可能是未来的MVC /版本的WebAPI会间preT标记为超时任的客户端断开连接。

You should use it. Right now it only applies if you have an AsyncTimeout, but it's likely that a future MVC/WebAPI version will interpret the token as "either timeout or the client disconnected".

这篇关于我们是否应该使用的CancellationToken与MVC /网页API控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆