diff --git a/sdk/core/Azure.Core.TestFramework/src/Instrumentation/ManagementInterceptor.cs b/sdk/core/Azure.Core.TestFramework/src/Instrumentation/ManagementInterceptor.cs index 53bd2a9308e1..dbc193e9ddfd 100644 --- a/sdk/core/Azure.Core.TestFramework/src/Instrumentation/ManagementInterceptor.cs +++ b/sdk/core/Azure.Core.TestFramework/src/Instrumentation/ManagementInterceptor.cs @@ -2,6 +2,7 @@ // Licensed under the MIT License. using System; +using System.Linq; using System.Reflection; using System.Threading.Tasks; using Castle.DynamicProxy; @@ -39,11 +40,25 @@ public void Intercept(IInvocation invocation) var taskResultType = type.GetGenericArguments()[0]; if (taskResultType.Name.StartsWith("Response")) { - var taskResult = result.GetType().GetProperty("Result").GetValue(result); - var instrumentedResult = _testBase.InstrumentClient(taskResultType, taskResult, new IInterceptor[] { new ManagementInterceptor(_testBase) }); - invocation.ReturnValue = type.Name.StartsWith("ValueTask") - ? GetValueFromValueTask(taskResultType, instrumentedResult) - : GetValueFromOther(taskResultType, instrumentedResult); + try + { + var taskResult = result.GetType().GetProperty("Result").GetValue(result); + var instrumentedResult = _testBase.InstrumentClient(taskResultType, taskResult, new IInterceptor[] { new ManagementInterceptor(_testBase) }); + invocation.ReturnValue = type.Name.StartsWith("ValueTask") + ? GetValueFromValueTask(taskResultType, instrumentedResult) + : GetValueFromOther(taskResultType, instrumentedResult); + } + catch (TargetInvocationException e) + { + if (e.InnerException is AggregateException aggException) + { + throw aggException.InnerExceptions.First(); + } + else + { + throw e.InnerException; + } + } } } else if (invocation.Method.Name.EndsWith("Value") && type.BaseType.Name.EndsWith("Operations"))