Skip to content

Commit 10b59e2

Browse files
committed
fix(IE.Excel): 图片下载改 per-call HttpClient 修并发回归
f27f28f 把 WebClient 换成共享 static HttpClient 后,跨 class 并行测试 并发下载偶发失败,异常被上层 catch 吞掉导致 drawing 缺失。改为 per-call using new HttpClient,30s 超时保留。
1 parent 398b51f commit 10b59e2

1 file changed

Lines changed: 7 additions & 7 deletions

File tree

src/Magicodes.ExporterAndImporter.Excel/Images/ImageExtensions.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@ namespace Magicodes.IE.Excel.Images
99
{
1010
public static partial class ImageExtensions
1111
{
12-
private static readonly HttpClient _httpClient = new HttpClient
13-
{
14-
Timeout = TimeSpan.FromSeconds(30)
15-
};
16-
1712
public struct ImageInfo
1813
{
1914
public int Width;
@@ -42,7 +37,11 @@ public static ImageInfo IdentifyImage(byte[] imageBytes)
4237

4338
public static byte[] DownloadImageBytes(this string url)
4439
{
45-
return _httpClient.GetByteArrayAsync(url).ConfigureAwait(false).GetAwaiter().GetResult();
40+
// per-call 实例:避免共享 static HttpClient 在并发下载下偶发失败导致图片静默丢失
41+
using (var http = new HttpClient { Timeout = TimeSpan.FromSeconds(30) })
42+
{
43+
return http.GetByteArrayAsync(url).ConfigureAwait(false).GetAwaiter().GetResult();
44+
}
4645
}
4746

4847
public static byte[] ReadImageBytes(this string filePath)
@@ -73,7 +72,8 @@ private static string GetContentType(SKEncodedImageFormat format)
7372

7473
public static SixLabors.ImageSharp.Image GetImageByUrl(this string url, out SixLabors.ImageSharp.Formats.IImageFormat format)
7574
{
76-
using (Stream webStream = _httpClient.GetStreamAsync(url).ConfigureAwait(false).GetAwaiter().GetResult())
75+
using (var http = new HttpClient { Timeout = TimeSpan.FromSeconds(30) })
76+
using (Stream webStream = http.GetStreamAsync(url).ConfigureAwait(false).GetAwaiter().GetResult())
7777
{
7878
using (MemoryStream memoryStream = new MemoryStream())
7979
{

0 commit comments

Comments
 (0)