Should we use CancellationToken with MVC/Web API controllers?

后端 未结 3 1545
轮回少年
轮回少年 2020-12-29 17:51

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

public async Task ShowItem(i         


        
3条回答
  •  一整个雨季
    2020-12-29 18:36

    You could use this

    public async Task MyReallySlowReport(CancellationToken cancellationToken)
    {
        CancellationToken disconnectedToken = Response.ClientDisconnectedToken;
        using (var source = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, disconnectedToken))
        {
            IEnumerable items;
            using (ApplicationDbContext context = new ApplicationDbContext())
            {
                items = await context.ReportItems.ToArrayAsync(source.Token);
            }
            return View(items);
        }
    }
    

    taken from here.

提交回复
热议问题