/// <summary> /// Converts the <see cref="ITask{TResult}"/> instance into a <see cref="Task{TResult}"/>. /// </summary> /// <param name="task"> /// The task to convert. /// </param> /// <typeparam name="TResult"> /// The type of the result of the task. /// </typeparam> /// <returns> /// The <see cref="Task{TResult}"/>. /// </returns> public static async Task <TResult> AsTask <TResult>(this ITask <TResult> task) { if (task == null) { throw new ArgumentNullException(nameof(task)); } return(await task.ConfigureAwait(false)); }
/// <summary> /// Converts the <see cref="ITask"/> instance into a <see cref="Task"/>. /// </summary> /// <param name="task"> /// The task to convert. /// </param> /// <returns> /// The <see cref="Task"/>. /// </returns> public static async Task AsTask(this ITask task) { if (task == null) { throw new ArgumentNullException(nameof(task)); } await task.ConfigureAwait(false); }
/// <summary> /// 当请求异常时返回默认值 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="task"></param> /// <returns></returns> public static async Task <T> ExceptionThenDefault <T>(this ITask <T> task) { try { return(await task.ConfigureAwait(false)); } catch (Exception) { return(default);
private static async ITask <int> UseGenericITaskAsync() { ITask <int> task = TaskAdapter <int> .Factory.StartNew(() => 43); var result = await task.ConfigureAwait(false); Console.WriteLine($"ITask<int> returned {result}"); return(result); }
/// <summary> /// Converts the <see cref="ITask{TResult}"/> instance into a <see cref="Task{TResult}"/>. /// </summary> /// <param name="task"> /// The task to convert. /// </param> /// <typeparam name="TResult"> /// The type of the result of the task. /// </typeparam> /// <returns> /// The <see cref="Task{TResult}"/>. /// </returns> public static async Task <TResult> AsTask <TResult>(this ITask <TResult> task) { return(await task.ConfigureAwait(false)); }
/// <summary> /// Converts the <see cref="ITask"/> instance into a <see cref="Task"/>. /// </summary> /// <param name="task"> /// The task to convert. /// </param> /// <returns> /// The <see cref="Task"/>. /// </returns> public static async Task AsTask(this ITask task) { await task.ConfigureAwait(false); }
/// <summary> /// 保存到目标流 /// </summary> /// <param name="response"></param> /// <param name="targetStream">流</param> /// <exception cref="ArgumentNullException"></exception> /// <returns></returns> public static async Task SaveAsync(this ITask <HttpResponseMessage> response, Stream targetStream) { var res = await response.ConfigureAwait(false); await res.SaveAsync(targetStream).ConfigureAwait(false); }
/// <summary> /// 保存到目标文件 /// </summary> /// <param name="response"></param> /// <param name="filePath">文件路径</param> /// <exception cref="ArgumentNullException"></exception> /// <returns></returns> public static async Task SaveAsync(this ITask <HttpResponseMessage> response, string filePath) { var res = await response.ConfigureAwait(false); await res.SaveAsync(filePath).ConfigureAwait(false); }