From 6a6b43fe9a17df9352cd336129131ff8cb96d346 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BE=99=E8=85=BE=E7=8C=AB=E8=B7=83?= <1043137532@qq.com> Date: Thu, 25 Jun 2026 07:30:00 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BF=81=E7=A7=BB=E9=83=A8=E5=88=86=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E5=88=B0=20TaskExtensions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Shared/Extensions/ExceptionExtensions.cs | 2 +- Shared/Extensions/TaskExtensions.cs | 10 ++++++++++ Shared/Extensions/WebExtensions.cs | 4 ++-- Shared/Utils/TaskUtils.cs | 3 --- 4 files changed, 13 insertions(+), 6 deletions(-) create mode 100644 Shared/Extensions/TaskExtensions.cs diff --git a/Shared/Extensions/ExceptionExtensions.cs b/Shared/Extensions/ExceptionExtensions.cs index e6bd175..e1daebb 100644 --- a/Shared/Extensions/ExceptionExtensions.cs +++ b/Shared/Extensions/ExceptionExtensions.cs @@ -23,7 +23,7 @@ public static class ExceptionExtensions { /// /// 当该异常或其子异常是 、 或 时,抛出对应异常。 /// - public static void ThrowOnCanceled(this Exception ex) { + public static void ThrowIfCanceled(this Exception ex) { foreach (var inner in ex.Flatten()) { if (inner is OperationCanceledException or TaskCanceledException or ThreadInterruptedException) throw inner; } diff --git a/Shared/Extensions/TaskExtensions.cs b/Shared/Extensions/TaskExtensions.cs new file mode 100644 index 0000000..d6d1e28 --- /dev/null +++ b/Shared/Extensions/TaskExtensions.cs @@ -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(this Task task) => task.GetAwaiter().GetResult(); + + public static ConfiguredTaskAwaitable NoCapture(this Task task) => task.ConfigureAwait(false); + public static ConfiguredTaskAwaitable NoCapture(this Task task) => task.ConfigureAwait(false); + +} diff --git a/Shared/Extensions/WebExtensions.cs b/Shared/Extensions/WebExtensions.cs index fa046da..0b933f3 100644 --- a/Shared/Extensions/WebExtensions.cs +++ b/Shared/Extensions/WebExtensions.cs @@ -13,7 +13,7 @@ public static class WebExtensions { } public static async Task CloneAsync(this HttpRequestMessage request) { var clone = new HttpRequestMessage(request.Method, request.RequestUri) { - Content = await request.Content.CloneAsync().ConfigureAwait(false), + Content = await request.Content.CloneAsync().NoCapture(), Version = request.Version }; foreach (KeyValuePair prop in request.Properties) clone.Properties.Add(prop); @@ -32,7 +32,7 @@ public static class WebExtensions { public static async Task CloneAsync(this HttpContent? content) { if (content == null) return null; var ms = new MemoryStream(); - await content.CopyToAsync(ms).ConfigureAwait(false); + await content.CopyToAsync(ms).NoCapture(); ms.Position = 0; var clone = new StreamContent(ms); foreach (KeyValuePair> header in content.Headers) clone.Headers.Add(header.Key, header.Value); diff --git a/Shared/Utils/TaskUtils.cs b/Shared/Utils/TaskUtils.cs index b9122d4..203ad29 100644 --- a/Shared/Utils/TaskUtils.cs +++ b/Shared/Utils/TaskUtils.cs @@ -3,9 +3,6 @@ using System.Diagnostics; namespace MeloongCore; public static class TaskUtils { - public static void Run(this Task task) => task.GetAwaiter().GetResult(); - public static T Run(this Task task) => task.GetAwaiter().GetResult(); - /// /// 静默运行程序并等待其结束,返回其输出和退出码。 /// 支持 notepad、git 等命令。