diff --git a/src/System.Windows.Forms.Primitives/src/System/LocalAppContextSwitches/LocalAppContextSwitches.cs b/src/System.Windows.Forms.Primitives/src/System/LocalAppContextSwitches/LocalAppContextSwitches.cs index ae006152b9a..48a3ab0cdcf 100644 --- a/src/System.Windows.Forms.Primitives/src/System/LocalAppContextSwitches/LocalAppContextSwitches.cs +++ b/src/System.Windows.Forms.Primitives/src/System/LocalAppContextSwitches/LocalAppContextSwitches.cs @@ -18,7 +18,6 @@ internal static partial class LocalAppContextSwitches private const string ScaleTopLevelFormMinMaxSizeForDpiSwitchName = "System.Windows.Forms.ScaleTopLevelFormMinMaxSizeForDpi"; internal const string AnchorLayoutV2SwitchName = "System.Windows.Forms.AnchorLayoutV2"; internal const string ApplyParentFontToMenusSwitchName = "System.Windows.Forms.ApplyParentFontToMenus"; - internal const string ServicePointManagerCheckCrlSwitchName = "System.Windows.Forms.ServicePointManagerCheckCrl"; internal const string TrackBarModernRenderingSwitchName = "System.Windows.Forms.TrackBarModernRendering"; private const string DoNotCatchUnhandledExceptionsSwitchName = "System.Windows.Forms.DoNotCatchUnhandledExceptions"; internal const string DataGridViewUIAStartRowCountAtZeroSwitchName = "System.Windows.Forms.DataGridViewUIAStartRowCountAtZero"; @@ -29,7 +28,6 @@ internal static partial class LocalAppContextSwitches private static int s_scaleTopLevelFormMinMaxSizeForDpi; private static int s_anchorLayoutV2; private static int s_applyParentFontToMenus; - private static int s_servicePointManagerCheckCrl; private static int s_trackBarModernRendering; private static int s_doNotCatchUnhandledExceptions; private static int s_dataGridViewUIAStartRowCountAtZero; @@ -124,11 +122,6 @@ static bool GetSwitchDefaultValue(string switchName) { return true; } - - if (switchName == ServicePointManagerCheckCrlSwitchName) - { - return true; - } } return false; @@ -171,17 +164,6 @@ public static bool TrackBarModernRendering get => GetCachedSwitchValue(TrackBarModernRenderingSwitchName, ref s_trackBarModernRendering); } - /// - /// Indicates whether certificates are checked against the certificate authority revocation list. - /// If true, revoked certificates will not be accepted by WebRequests and WebClients as valid. - /// Otherwise, revoked certificates will be accepted as valid. - /// - public static bool ServicePointManagerCheckCrl - { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - get => GetCachedSwitchValue(ServicePointManagerCheckCrlSwitchName, ref s_servicePointManagerCheckCrl); - } - public static bool DataGridViewUIAStartRowCountAtZero { [MethodImpl(MethodImplOptions.AggressiveInlining)] diff --git a/src/System.Windows.Forms.Primitives/tests/UnitTests/System/Windows/Forms/LocalAppContextSwitchesTest.cs b/src/System.Windows.Forms.Primitives/tests/UnitTests/System/Windows/Forms/LocalAppContextSwitchesTest.cs index 1fbae5a7a54..1b0b4c1636f 100644 --- a/src/System.Windows.Forms.Primitives/tests/UnitTests/System/Windows/Forms/LocalAppContextSwitchesTest.cs +++ b/src/System.Windows.Forms.Primitives/tests/UnitTests/System/Windows/Forms/LocalAppContextSwitchesTest.cs @@ -13,7 +13,6 @@ private void ResetLocalSwitches(dynamic testAccessor) testAccessor.s_anchorLayoutV2 = 0; testAccessor.s_scaleTopLevelFormMinMaxSizeForDpi = 0; testAccessor.s_trackBarModernRendering = 0; - testAccessor.s_servicePointManagerCheckCrl = 0; } [WinFormsTheory] @@ -32,7 +31,6 @@ public void Validate_Default_Switch_Values(string targetFrameworkName, bool expe Assert.Equal(expected, LocalAppContextSwitches.TrackBarModernRendering); Assert.Equal(expected, LocalAppContextSwitches.ScaleTopLevelFormMinMaxSizeForDpi); - Assert.Equal(expected, LocalAppContextSwitches.ServicePointManagerCheckCrl); } finally { 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 b756286ff14..7bd145f42a7 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 @@ -7,7 +7,6 @@ using System.Net; using System.Runtime.InteropServices; using System.Windows.Forms.Layout; -using System.Windows.Forms.Primitives; namespace System.Windows.Forms; @@ -62,6 +61,7 @@ public partial class PictureBox : Control, ISupportInitialize private SendOrPostCallback? _loadProgressDelegate; private bool _handleValid; private readonly object _internalSyncObject = new(); + private readonly HttpClient _httpClient = new(); // These default images will be demand loaded. private Image? _defaultInitialImage; @@ -464,23 +464,29 @@ 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) + Task.Run(async () => { - _localImageStreamReader = new StreamReader(uri.LocalPath); - Image img = Image.FromStream(_localImageStreamReader.BaseStream); + HttpResponseMessage response = await _httpClient.GetAsync(uri).ConfigureAwait(false); + response.EnsureSuccessStatusCode(); + + using Stream stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false); + _uriImageStream = stream; + 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,32 +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) - { - ServicePointManager.CheckCertificateRevocationList = true; - } - -#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) diff --git a/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/PictureBoxTests.cs b/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/PictureBoxTests.cs index 553d2ceb0c5..fb11d869f2b 100644 --- a/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/PictureBoxTests.cs +++ b/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/PictureBoxTests.cs @@ -732,17 +732,13 @@ public void PictureBox_ImageLocation_SetValidWithWaitOnLoadTrueLocal_GetReturnsE Assert.Equal(PathImageLocation, pictureBox.ImageLocation); } - [WinFormsTheory] - [BoolData] - public void PictureBox_ImageLocation_SetValidWithWaitOnLoadTrueUri_ConfigSwitch_CheckCRL_GetReturnsExpected(bool switchValue) + [WinFormsFact] + public void PictureBox_ImageLocation_SetValidWithWaitOnLoadTrueUri_ConfigSwitch_CheckCRL_GetReturnsExpected() { dynamic testAccessor = typeof(LocalAppContextSwitches).TestAccessor().Dynamic; try { - AppContext.SetSwitch(LocalAppContextSwitches.ServicePointManagerCheckCrlSwitchName, switchValue); - Assert.Equal(switchValue, LocalAppContextSwitches.ServicePointManagerCheckCrl); - using PictureBox pictureBox = new() { WaitOnLoad = true @@ -763,10 +759,6 @@ public void PictureBox_ImageLocation_SetValidWithWaitOnLoadTrueUri_ConfigSwitch_ { // Swallow network errors. } - finally - { - testAccessor.s_servicePointManagerCheckCrl = 0; - } } [WinFormsTheory] @@ -2002,17 +1994,13 @@ public void PictureBox_Load_UrlValidWithWaitOnLoadTrueLocal_GetReturnsExpected() Assert.Equal(new Size(110, 100), pictureBox.Image.Size); } - [WinFormsTheory] - [BoolData] - public void PictureBox_Load_UrlValidWithWaitOnLoadTrueUri_ConfigSwitch_CheckCRL_GetReturnsExpected(bool switchValue) + [WinFormsFact] + public void PictureBox_Load_UrlValidWithWaitOnLoadTrueUri_ConfigSwitch_CheckCRL_GetReturnsExpected() { dynamic testAccessor = typeof(LocalAppContextSwitches).TestAccessor().Dynamic; try { - AppContext.SetSwitch(LocalAppContextSwitches.ServicePointManagerCheckCrlSwitchName, switchValue); - Assert.Equal(switchValue, LocalAppContextSwitches.ServicePointManagerCheckCrl); - using PictureBox pictureBox = new() { WaitOnLoad = true @@ -2033,10 +2021,6 @@ public void PictureBox_Load_UrlValidWithWaitOnLoadTrueUri_ConfigSwitch_CheckCRL_ { // Swallow network errors. } - finally - { - testAccessor.s_servicePointManagerCheckCrl = 0; - } } [WinFormsTheory]