1 //------------------------------------------------------------------------------
2 // <copyright file="TaskExtensions.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
5 //------------------------------------------------------------------------------
7 namespace System.Web.Util {
9 using System.Threading.Tasks;
11 // Contains helper methods for dealing with Tasks
13 internal static class TaskExtensions {
15 // Throws the exception that faulted a Task, similar to what 'await' would have done.
16 // Useful for synchronous methods which have a Task instance they know to be already completed
17 // and where they want to let the exception propagate upward.
18 public static void ThrowIfFaulted(this Task task) {
19 Debug.Assert(task.IsCompleted, "The Task passed to this method must be marked as completed so that this method doesn't block.");
20 task.GetAwaiter().GetResult();
23 // Gets a WithinCancellableCallbackTaskAwaiter from a Task.
24 public static WithinCancellableCallbackTaskAwaitable WithinCancellableCallback(this Task task, HttpContext context) {
25 return new WithinCancellableCallbackTaskAwaitable(context, task.GetAwaiter());