迁移部分方法到 TaskExtensions

This commit is contained in:
龙腾猫跃 committed 2026-06-25 07:30:00 +08:00
1 parent 1a639fa88f
commit 6a6b43fe9a
4 files changed
+13 -6

No files matched your search

+1 -1
View File
@@ -23,7 +23,7 @@ public static class ExceptionExtensions {
/// <summary> /// <summary>
/// 当该异常或其子异常是 <see cref="OperationCanceledException"/>、<see cref="TaskCanceledException"/> 或 <see cref="ThreadInterruptedException"/> 时,抛出对应异常。 /// 当该异常或其子异常是 <see cref="OperationCanceledException"/>、<see cref="TaskCanceledException"/> 或 <see cref="ThreadInterruptedException"/> 时,抛出对应异常。
/// </summary> /// </summary>
public static void ThrowOnCanceled(this Exception ex) { public static void ThrowIfCanceled(this Exception ex) {
foreach (var inner in ex.Flatten()) { foreach (var inner in ex.Flatten()) {
if (inner is OperationCanceledException or TaskCanceledException or ThreadInterruptedException) throw inner; if (inner is OperationCanceledException or TaskCanceledException or ThreadInterruptedException) throw inner;
} }
+10
View File
@@ -0,0 +1,10 @@
namespace MeloongCore.Extensions;
public static class TaskExtensions {
public static void Run(this Task task) => task.GetAwaiter().GetResult();
public static T Run<T>(this Task<T> task) => task.GetAwaiter().GetResult();
public static ConfiguredTaskAwaitable NoCapture(this Task task) => task.ConfigureAwait(false);
public static ConfiguredTaskAwaitable<T> NoCapture<T>(this Task<T> task) => task.ConfigureAwait(false);
}
+2 -2
View File
@@ -13,7 +13,7 @@ public static class WebExtensions {
} }
public static async Task<HttpRequestMessage> CloneAsync(this HttpRequestMessage request) { public static async Task<HttpRequestMessage> CloneAsync(this HttpRequestMessage request) {
var clone = new HttpRequestMessage(request.Method, request.RequestUri) { var clone = new HttpRequestMessage(request.Method, request.RequestUri) {
Content = await request.Content.CloneAsync().ConfigureAwait(false), Content = await request.Content.CloneAsync().NoCapture(),
Version = request.Version Version = request.Version
}; };
foreach (KeyValuePair<string, object> prop in request.Properties) clone.Properties.Add(prop); foreach (KeyValuePair<string, object> prop in request.Properties) clone.Properties.Add(prop);
@@ -32,7 +32,7 @@ public static class WebExtensions {
public static async Task<HttpContent?> CloneAsync(this HttpContent? content) { public static async Task<HttpContent?> CloneAsync(this HttpContent? content) {
if (content == null) return null; if (content == null) return null;
var ms = new MemoryStream(); var ms = new MemoryStream();
await content.CopyToAsync(ms).ConfigureAwait(false); await content.CopyToAsync(ms).NoCapture();
ms.Position = 0; ms.Position = 0;
var clone = new StreamContent(ms); var clone = new StreamContent(ms);
foreach (KeyValuePair<string, IEnumerable<string>> header in content.Headers) clone.Headers.Add(header.Key, header.Value); foreach (KeyValuePair<string, IEnumerable<string>> header in content.Headers) clone.Headers.Add(header.Key, header.Value);
-3
View File
@@ -3,9 +3,6 @@ using System.Diagnostics;
namespace MeloongCore; namespace MeloongCore;
public static class TaskUtils { public static class TaskUtils {
public static void Run(this Task task) => task.GetAwaiter().GetResult();
public static T Run<T>(this Task<T> task) => task.GetAwaiter().GetResult();
/// <summary> /// <summary>
/// 静默运行程序并等待其结束,返回其输出和退出码。 /// 静默运行程序并等待其结束,返回其输出和退出码。
/// 支持 notepad、git 等命令。 /// 支持 notepad、git 等命令。