14

ASP.NET Core 2.0の非同期ビュー コンポーネントを作成しようとしています。ユーザーがページから離れたときにキャンセルするアクションを実行します。次のオプションがあります。

  1. HttpContext.RequestAborted の使用
  2. CancellationToken パラメーターの使用
  3. トークンをチェーンすることもできます

オプション 1 は次のようになります。

public class AmazingMessageViewComponent : ViewComponent
{
    public async Task<IViewComponentResult> InvokeAsync(string text, int wait)
    {
        //uses request aborted
        await Task.Delay(wait, HttpContext.RequestAborted);
        return View<string>(text);
    }
}

オプション 2 は次のようになります。

public class AmazingMessageViewComponent : ViewComponent
{
    public async Task<IViewComponentResult> InvokeAsync(CancellationToken cancellationToken, string text, int wait)
    {
        await Task.Delay(wait, cancellationToken);
        return View<string>(text);
    }
}

どちらのアクションも Kestrel では機能しません(バグのようです)。どちらの場合も、トークンは満たされています (おそらく構造体が原因でしょうか?)

違いは何ですか?何を使用すればよいですか?

4

1 に答える 1