コード例 #1
0
        public void Intercept(IInvocation invocation)
        {
            var type = invocation.Method.ReturnType;

            // We don't want to instrument generated rest clients.
            if ((type.Name.EndsWith("Client") && !type.Name.EndsWith("RestClient")) ||
                // Generated ARM clients will have a property containing the sub-client that ends with Operations.
                (invocation.Method.Name.StartsWith("get_") && type.Name.EndsWith("Operations")))
            {
                if (IsNullResult(invocation))
                {
                    return;
                }

                invocation.ReturnValue = _testBase.InstrumentClient(type, invocation.ReturnValue, Array.Empty <IInterceptor>());
                return;
            }

            if (
                // Generated ARM clients will have a property containing the sub-client that ends with Operations.
                (invocation.Method.Name.StartsWith("get_") && (type.Name.EndsWith("Operations") || (type.BaseType != null && type.BaseType.Name.EndsWith("Operations")))) ||
                // Instrument the container construction methods inside Operations objects
                (invocation.Method.Name.StartsWith("Get") && type.Name.EndsWith("Container")) ||
                // Instrument the operations construction methods inside Operations objects
                (invocation.Method.Name.StartsWith("Get") && type.Name.EndsWith("Operations")))
            {
                if (IsNullResult(invocation))
                {
                    return;
                }

                invocation.ReturnValue = _testBase.InstrumentClient(type, invocation.ReturnValue, new IInterceptor[] { new ManagementInterceptor(_testBase) });
                return;
            }

            if (type is { IsGenericType : true, GenericTypeArguments : {} arguments } &&
コード例 #2
0
        public void Intercept(IInvocation invocation)
        {
            var type = invocation.Method.ReturnType;

            // We don't want to instrument generated rest clients.
            if ((type.Name.EndsWith("Client") && !type.Name.EndsWith("RestClient") && !type.Name.EndsWith("ExtensionClient")) ||
                // Generated ARM clients will have a property containing the sub-client that ends with Operations.
                //TODO: remove after all track2 .net mgmt libraries are updated to the new generation
                (invocation.Method.Name.StartsWith("get_") && type.Name.EndsWith("Operations")))
            {
                if (IsNullResult(invocation))
                {
                    return;
                }

                invocation.ReturnValue = _testBase.InstrumentClient(type, invocation.ReturnValue, Array.Empty <IInterceptor>());
                return;
            }

            if (
                // Generated ARM clients will have a property containing the sub-client that ends with Operations.
                (invocation.Method.Name.StartsWith("get_") && ManagementInterceptor.InheritsFromArmResource(type)) ||
                // Instrument the container construction methods inside Operations objects
                // Instrument the operations construction methods inside Operations objects
                (invocation.Method.Name.StartsWith("Get") && ManagementInterceptor.InheritsFromArmResource(type)))
            {
                if (IsNullResult(invocation))
                {
                    return;
                }

                invocation.ReturnValue = _testBase.InstrumentClient(type, invocation.ReturnValue, new IInterceptor[] { new ManagementInterceptor(_testBase) });
                return;
            }

            if (type is { IsGenericType : true, GenericTypeArguments : {} arguments } &&
コード例 #3
0
        public void Intercept(IInvocation invocation)
        {
            invocation.Proceed();

            var result = invocation.ReturnValue;

            if (result == null)
            {
                return;
            }

            var type = result.GetType();

            if (type.Name.EndsWith("Client"))
            {
                invocation.ReturnValue = _testBase.InstrumentClient(type, result, Array.Empty <IInterceptor>());
            }
        }
        public void Intercept(IInvocation invocation)
        {
            invocation.Proceed();

            var result = invocation.ReturnValue;

            if (result == null)
            {
                return;
            }

            var type = result.GetType();

            if (type.Name.EndsWith("Client") ||
                // Generated ARM clients will have a property containing the sub-client that ends with Operations.
                (invocation.Method.Name.StartsWith("get_") && type.Name.EndsWith("Operations")))
            {
                invocation.ReturnValue = _testBase.InstrumentClient(type, result, Array.Empty <IInterceptor>());
            }
        }
コード例 #5
0
        public void Intercept(IInvocation invocation)
        {
            var type = invocation.Method.ReturnType;

            // We don't want to instrument generated rest clients.
            if ((type.Name.EndsWith("Client") && !type.Name.EndsWith("RestClient")) ||
                // Generated ARM clients will have a property containing the sub-client that ends with Operations.
                (invocation.Method.Name.StartsWith("get_") && type.Name.EndsWith("Operations")))
            {
                invocation.Proceed();

                var result = invocation.ReturnValue;
                if (result == null)
                {
                    return;
                }

                invocation.ReturnValue = _testBase.InstrumentClient(type, result, Array.Empty <IInterceptor>());
                return;
            }

            if (type is { IsGenericType : true, GenericTypeArguments : {} arguments } &&
コード例 #6
0
        public void Intercept(IInvocation invocation)
        {
            invocation.Proceed();

            var result = invocation.ReturnValue;

            if (result == null)
            {
                return;
            }

            var type = result.GetType();

            if (type.Name.StartsWith("ValueTask") ||
                type.Name.StartsWith("Task") ||
                type.Name.StartsWith("AsyncStateMachineBox")) //in .net 5 the type is not task here
            {
                if ((bool)type.GetProperty("IsFaulted").GetValue(result))
                {
                    return;
                }

                var taskResultType = type.GetGenericArguments()[0];
                if (taskResultType.Name.StartsWith("Response"))
                {
                    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"))
            {
                invocation.ReturnValue = _testBase.InstrumentClient(type, result, new IInterceptor[] { new ManagementInterceptor(_testBase) });
            }
            else if (type.BaseType.Name.StartsWith("AsyncPageable"))
            {
                invocation.ReturnValue = s_proxyGenerator.CreateClassProxyWithTarget(type, result, new IInterceptor[] { new ManagementInterceptor(_testBase) });
            }
            else if (invocation.Method.Name.StartsWith("Get") &&
                     invocation.Method.Name.EndsWith("Enumerator") &&
                     type.IsGenericType &&
                     InheritsFromOperationBase(type.GetGenericArguments().First()))
            {
                var wrapperType = typeof(AsyncPageableInterceptor <>);
                var genericType = wrapperType.MakeGenericType(type.GetGenericArguments()[0]);
                var ctor        = genericType.GetConstructor(new Type[] { typeof(ClientTestBase), result.GetType() });
                invocation.ReturnValue = ctor.Invoke(new object[] { _testBase, result });
            }
        }
コード例 #7
0
        public void Intercept(IInvocation invocation)
        {
            bool modifiedAskToWait = false;

            if (IsLro(invocation.Method.ReturnType))
            {
                WaitUntil current = (WaitUntil)invocation.Arguments[0];
                if (current == WaitUntil.Completed)
                {
                    modifiedAskToWait       = true;
                    invocation.Arguments[0] = WaitUntil.Started;
                }
            }

            invocation.Proceed();

            if (modifiedAskToWait)
            {
                if (IsTaskFaulted(invocation.ReturnValue))
                {
                    return;
                }
                object lro = GetResultFromTask(invocation.ReturnValue);
                if (lro.GetType().IsSubclassOf(typeof(Operation)))
                {
                    _ = OperationInterceptor.InvokeWaitForCompletionResponse(lro as Operation, (CancellationToken)invocation.Arguments.Last());
                }
                else
                {
                    _ = OperationInterceptor.InvokeWaitForCompletion(lro, lro.GetType(), (CancellationToken)invocation.Arguments.Last());
                }
                return;
            }

            var result = invocation.ReturnValue;

            if (result == null)
            {
                return;
            }

            var type = result.GetType();

            if (IsTaskType(type))
            {
                if (IsTaskFaulted(result))
                {
                    return;
                }

                var taskResultType = type.GetGenericArguments()[0];
                if (taskResultType.Name.StartsWith("Response") || InheritsFromArmResource(taskResultType))
                {
                    var taskResult         = GetResultFromTask(result);
                    var instrumentedResult = _testBase.InstrumentClient(taskResultType, taskResult, new IInterceptor[] { new ManagementInterceptor(_testBase) });
                    invocation.ReturnValue = type.Name.StartsWith("ValueTask")
                        ? GetValueFromValueTask(taskResultType, instrumentedResult)
                        : GetValueFromOther(taskResultType, instrumentedResult);
                }
            }
            else if (invocation.Method.Name.EndsWith("Value") && InheritsFromArmResource(type))
            {
                invocation.ReturnValue = _testBase.InstrumentClient(type, result, new IInterceptor[] { new ManagementInterceptor(_testBase) });
            }
            else if (type.BaseType.Name.StartsWith("AsyncPageable"))
            {
                invocation.ReturnValue = s_proxyGenerator.CreateClassProxyWithTarget(type, result, new IInterceptor[] { new ManagementInterceptor(_testBase) });
            }
            else if (invocation.Method.Name.StartsWith("Get") &&
                     invocation.Method.Name.EndsWith("Enumerator") &&
                     type.IsGenericType &&
                     InheritsFromArmResource(type.GetGenericArguments().First()))
            {
                var wrapperType = typeof(AsyncPageableInterceptor <>);
                var genericType = wrapperType.MakeGenericType(type.GetGenericArguments()[0]);
                var ctor        = genericType.GetConstructor(new Type[] { typeof(ClientTestBase), result.GetType() });
                invocation.ReturnValue = ctor.Invoke(new object[] { _testBase, result });
            }
        }