diff --git a/src/System.Net.Http/src/System/Net/Http/Managed/HttpConnectionHandler.cs b/src/System.Net.Http/src/System/Net/Http/Managed/HttpConnectionHandler.cs index 00e498a34065..67d901083b59 100644 --- a/src/System.Net.Http/src/System/Net/Http/Managed/HttpConnectionHandler.cs +++ b/src/System.Net.Http/src/System/Net/Http/Managed/HttpConnectionHandler.cs @@ -50,7 +50,14 @@ private async ValueTask EstablishSslConnection(string host, HttpReque { callback = (object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) => { - return _settings._serverCertificateCustomValidationCallback(request, certificate as X509Certificate2, chain, sslPolicyErrors); + try + { + return _settings._serverCertificateCustomValidationCallback(request, certificate as X509Certificate2, chain, sslPolicyErrors); + } + catch (Exception e) + { + throw new HttpRequestException(SR.net_http_ssl_connection_failed, e); + } }; } diff --git a/src/System.Net.Http/tests/FunctionalTests/HttpClientHandlerTest.ServerCertificates.cs b/src/System.Net.Http/tests/FunctionalTests/HttpClientHandlerTest.ServerCertificates.cs index 0389ac3d8c79..0280cb3c5bb9 100644 --- a/src/System.Net.Http/tests/FunctionalTests/HttpClientHandlerTest.ServerCertificates.cs +++ b/src/System.Net.Http/tests/FunctionalTests/HttpClientHandlerTest.ServerCertificates.cs @@ -208,11 +208,6 @@ public async Task UseCallback_CallbackReturnsFailure_ThrowsException() [ConditionalFact(nameof(BackendSupportsCustomCertificateHandling))] public async Task UseCallback_CallbackThrowsException_ExceptionPropagatesAsBaseException() { - if (ManagedHandlerTestHelpers.IsEnabled) - { - return; // TODO #21904: ManagedHandler is not properly wrapping exception. - } - if (BackendDoesNotSupportCustomCertificateHandling) // can't use [Conditional*] right now as it's evaluated at the wrong time for the managed handler { Console.WriteLine($"Skipping {nameof(UseCallback_CallbackThrowsException_ExceptionPropagatesAsBaseException)}()");