File tree Expand file tree Collapse file tree
src/Magicodes.ExporterAndImporter.Excel/Images Expand file tree Collapse file tree Original file line number Diff line number Diff 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 {
You can’t perform that action at this time.
0 commit comments