예제 #1
0
        public async Task <bool> TryCallOperation(IPortalTarget target, PortalOperation operation)
        {
            await CheckAccess(operation.ToAuthorizationOperation());

            var methods = MethodsForOperation(operation) ?? new List <MethodInfo>();

            using (await target.StopAllActions())
            {
                var invoked = false;

                foreach (var method in methods)
                {
                    var success         = true;
                    var parameters      = method.GetParameters().ToList();
                    var parameterValues = new object[parameters.Count()];

                    for (var i = 0; i < parameterValues.Length; i++)
                    {
                        var parameter = parameters[i];
                        if (!Scope.IsRegistered(parameter.ParameterType))
                        {
                            // Assume it's a criteria not a dependency
                            success = false;
                            break;
                        }
                    }

                    if (success)
                    {
                        // No parameters or all of the parameters are dependencies
                        for (var i = 0; i < parameterValues.Length; i++)
                        {
                            var parameter = parameters[i];
                            parameterValues[i] = Scope.Resolve(parameter.ParameterType);
                        }

                        invoked = true;

                        var result = method.Invoke(target, parameterValues);
                        if (method.ReturnType == typeof(Task))
                        {
                            await(Task) result;
                        }

                        PostOperation(target, operation);

                        break;
                    }
                }

                return(invoked);
            }
        }
예제 #2
0
        public async Task <bool> TryCallOperation(IPortalTarget target, PortalOperation operation, object[] criteria, Type[] criteriaTypes)
        {
            await CheckAccess(operation.ToAuthorizationOperation(), criteria);

            using (await target.StopAllActions())
            {
                // The criteriaTypes need to be captured by Generic method definitions
                // in case the values sent in are null

                var method = MethodForOperation(operation, criteriaTypes);

                if (method != null)
                {
                    var parameters      = method.GetParameters().ToList();
                    var parameterValues = new object[parameters.Count()];

                    var criteriaE = criteria.GetEnumerator();

                    for (var i = 0; i < parameterValues.Length; i++)
                    {
                        if (criteriaE.MoveNext())
                        {
                            // Use up the criteria values first
                            // Assume MethodForOperation got the types right
                            parameterValues[i] = criteriaE.Current;
                        }
                        else
                        {
                            var parameter = parameters[i];
                            if (Scope.TryResolve(parameter.ParameterType, out var pv))
                            {
                                parameterValues[i] = pv;
                            }
                        }
                    }

                    var result = method.Invoke(target, parameterValues);

                    if (method.ReturnType == typeof(Task))
                    {
                        await(Task) result;
                    }

                    PostOperation(target, operation);

                    return(true);
                }

                return(false);
            }
        }