From c5b41e9ec2a88a8bcf1cb9c8ff7943095b38ec1d Mon Sep 17 00:00:00 2001 From: Steve Pfister Date: Mon, 26 Jul 2021 17:42:42 -0400 Subject: [PATCH] Have HttpClientHandler throw the InnerException when catching TargetInvocationException Originally on mobile workloads when UseNativeHttpHandler is set to true, all reflection method invokes bubbled up a TargetInvocationException. To make the details a bit more readable, we will instead rethrow the InnerException. Fixes https://github.com/dotnet/runtime/issues/56089 --- .../System/Net/Http/HttpClientHandler.AnyMobile.cs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.AnyMobile.cs b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.AnyMobile.cs index 309e60f65a9156..cfe17837ce96c3 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.AnyMobile.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.AnyMobile.cs @@ -9,6 +9,7 @@ using System.Globalization; using System.Net.Security; using System.Reflection; +using System.Runtime.ExceptionServices; using System.Runtime.Versioning; using System.Security.Authentication; using System.Security.Cryptography.X509Certificates; @@ -424,7 +425,15 @@ private object InvokeNativeHandlerMethod(string name, params object?[] parameter s_cachedMethods[name] = method; } - return method!.Invoke(_nativeHandler, parameters)!; + try + { + return method!.Invoke(_nativeHandler, parameters)!; + } + catch (TargetInvocationException e) + { + ExceptionDispatchInfo.Capture(e.InnerException!).Throw(); + throw; + } } private static bool IsNativeHandlerEnabled => RuntimeSettingParser.QueryRuntimeSettingSwitch(