From c58655532aa2ff81dc724742dfa22c336b76f559 Mon Sep 17 00:00:00 2001 From: kasperk81 <83082615+kasperk81@users.noreply.github.com> Date: Mon, 17 Jun 2024 19:21:41 +0300 Subject: [PATCH] use HttpClient to replace WebClient --- .../Forms/Controls/PictureBox/PictureBox.cs | 58 ++++++------------- 1 file changed, 18 insertions(+), 40 deletions(-) diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Controls/PictureBox/PictureBox.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Controls/PictureBox/PictureBox.cs index 90bbbcc9fb6..2855cb0b6f8 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Controls/PictureBox/PictureBox.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Controls/PictureBox/PictureBox.cs @@ -26,6 +26,9 @@ public partial class PictureBox : Control, ISupportInitialize !AppContext.TryGetSwitch("System.Windows.Forms.PictureBox.UseWebRequest", out bool useWebRequest) || useWebRequest; + private static readonly HttpClient s_httpClient = !LocalAppContextSwitches.ServicePointManagerCheckCrl ? new() : + new(new HttpClientHandler { CheckCertificateRevocationList = true }); + /// /// The type of border this control will have. /// @@ -464,23 +467,26 @@ public void Load() try { DisposeImageStream(); - if (UseWebRequest()) + Uri uri = CalculateUri(_imageLocation); + if (uri.IsFile) { - LoadImageViaWebClient(); + _localImageStreamReader = new StreamReader(uri.LocalPath); + Image img = Image.FromStream(_localImageStreamReader.BaseStream); + InstallNewImage(img, ImageInstallationType.FromUrl); } - else + else if (UseWebRequest()) { - Uri uri = CalculateUri(_imageLocation); - if (uri.IsFile) + // Run async operation synchronously to avoid blocking UI thread and potential deadlocks. + Task.Run(async () => { - _localImageStreamReader = new StreamReader(uri.LocalPath); - Image img = Image.FromStream(_localImageStreamReader.BaseStream); + _uriImageStream = await s_httpClient.GetStreamAsync(uri).ConfigureAwait(false); + Image img = Image.FromStream(_uriImageStream); InstallNewImage(img, ImageInstallationType.FromUrl); - } - else - { - throw new NotSupportedException(SR.PictureBoxRemoteLocationNotSupported); - } + }).GetAwaiter().GetResult(); + } + else + { + throw new NotSupportedException(SR.PictureBoxRemoteLocationNotSupported); } } catch @@ -497,34 +503,6 @@ public void Load() } } - private void LoadImageViaWebClient() - { - Image img; - Uri uri = CalculateUri(_imageLocation!); - if (uri.IsFile) - { - _localImageStreamReader = new StreamReader(uri.LocalPath); - img = Image.FromStream(_localImageStreamReader.BaseStream); - } - else - { - if (LocalAppContextSwitches.ServicePointManagerCheckCrl) - { -#pragma warning disable SYSLIB0014 // Type or member is obsolete - ServicePointManager.CheckCertificateRevocationList = true; -#pragma warning restore SYSLIB0014 - } - -#pragma warning disable SYSLIB0014 // Type or member is obsolete - using WebClient webClient = new(); // lgtm[cs/webrequest-checkcertrevlist-disabled] - Having ServicePointManager.CheckCertificateRevocationList set to true has a slim chance of resulting in failure. We have an opt-out for this rare event. -#pragma warning restore SYSLIB0014 // Type or member is obsolete - _uriImageStream = webClient.OpenRead(uri.ToString()); - img = Image.FromStream(_uriImageStream); - } - - InstallNewImage(img, ImageInstallationType.FromUrl); - } - [SRCategory(nameof(SR.CatAsynchronous))] [SRDescription(nameof(SR.PictureBoxLoad1Descr))] public void Load(string url)