internal ObjectMethodExecutor GetViewComponentMethodExecutor(ViewComponentContext viewComponentContext)
        {
            var cache = CurrentCache;
            var viewComponentDescriptor = viewComponentContext.ViewComponentDescriptor;

            if (cache.Entries.TryGetValue(viewComponentDescriptor, out var executor))
            {
                return(executor);
            }

            var methodInfo = viewComponentContext.ViewComponentDescriptor?.MethodInfo;

            if (methodInfo == null)
            {
                throw new InvalidOperationException(Resources.FormatPropertyOfTypeCannotBeNull(
                                                        nameof(ViewComponentDescriptor.MethodInfo),
                                                        nameof(ViewComponentDescriptor)));
            }

            var parameterDefaultValues = ParameterDefaultValues
                                         .GetParameterDefaultValues(methodInfo);

            executor = ObjectMethodExecutor.Create(
                viewComponentDescriptor.MethodInfo,
                viewComponentDescriptor.TypeInfo,
                parameterDefaultValues);

            cache.Entries.TryAdd(viewComponentDescriptor, executor);
            return(executor);
        }
        public static ObjectMethodExecutor CreateExecutor([NotNull] this MethodInfo methodInfo, [NotNull] Type type)
        {
            Check.NotNull(methodInfo, nameof(methodInfo));
            Check.NotNull(type, nameof(type));
            var parameterDefaultValues = ParameterDefaultValues.GetParameterDefaultValues(methodInfo);

            return(ObjectMethodExecutor.Create(methodInfo, type.GetTypeInfo(), parameterDefaultValues));
        }
Exemple #3
0
    public (ControllerActionInvokerCacheEntry cacheEntry, IFilterMetadata[] filters) GetCachedResult(ControllerContext controllerContext)
    {
        var actionDescriptor = controllerContext.ActionDescriptor;

        IFilterMetadata[] filters;

        var cacheEntry = actionDescriptor.CacheEntry;

        // We don't care about thread safety here
        if (cacheEntry is null)
        {
            var filterFactoryResult = FilterFactory.GetAllFilters(_filterProviders, controllerContext);
            filters = filterFactoryResult.Filters;

            var parameterDefaultValues = ParameterDefaultValues
                                         .GetParameterDefaultValues(actionDescriptor.MethodInfo);

            var objectMethodExecutor = ObjectMethodExecutor.Create(
                actionDescriptor.MethodInfo,
                actionDescriptor.ControllerTypeInfo,
                parameterDefaultValues);

            var controllerFactory     = _controllerFactoryProvider.CreateControllerFactory(actionDescriptor);
            var controllerReleaser    = _controllerFactoryProvider.CreateAsyncControllerReleaser(actionDescriptor);
            var propertyBinderFactory = ControllerBinderDelegateProvider.CreateBinderDelegate(
                _parameterBinder,
                _modelBinderFactory,
                _modelMetadataProvider,
                actionDescriptor,
                _mvcOptions);

            var actionMethodExecutor = ActionMethodExecutor.GetExecutor(objectMethodExecutor);
            var filterExecutor       = actionDescriptor.FilterDelegate is not null
                ? ActionMethodExecutor.GetFilterExecutor(actionDescriptor)
                : null;

            cacheEntry = new ControllerActionInvokerCacheEntry(
                filterFactoryResult.CacheableFilters,
                controllerFactory,
                controllerReleaser,
                propertyBinderFactory,
                objectMethodExecutor,
                filterExecutor ?? actionMethodExecutor,
                actionMethodExecutor);

            actionDescriptor.CacheEntry = cacheEntry;
        }
        else
        {
            // Filter instances from statically defined filter descriptors + from filter providers
            filters = FilterFactory.CreateUncachedFilters(_filterProviders, controllerContext, cacheEntry.CachedFilters);
        }

        return(cacheEntry, filters);
    }
    public void GetParameterDefaultValues_ReturnsExpectedValues(string methodName, object[] expectedValues)
    {
        // Arrange
        var methodInfo = typeof(TestObject).GetMethod(methodName);

        // Act
        var actualValues = ParameterDefaultValues.GetParameterDefaultValues(methodInfo);

        // Assert
        Assert.Equal(expectedValues, actualValues);
    }
    public void GetParameterDefaultValues_ReturnsExpectedValues_ForStructTypes()
    {
        // Arrange
        var methodInfo = typeof(TestObject).GetMethod("DefaultValuesOfStructTypes");

        // Act
        var actualValues = ParameterDefaultValues.GetParameterDefaultValues(methodInfo);

        // Assert
        Assert.Equal(
            new object[] { default(Guid), default(TimeSpan), default(DateTime), default(DateTimeOffset) },
            actualValues);
    }
Exemple #6
0
        private void ReConfiguration(IGovernanceProvider governanceProvider)
        {
            if (governanceProvider != null)
            {
                GovernanceOptions.CacheEnabled      = governanceProvider.CacheEnabled;
                GovernanceOptions.ExecutionTimeout  = governanceProvider.ExecutionTimeout;
                GovernanceOptions.FuseProtection    = governanceProvider.FuseProtection;
                GovernanceOptions.MaxConcurrent     = governanceProvider.MaxConcurrent;
                GovernanceOptions.ShuntStrategy     = governanceProvider.ShuntStrategy;
                GovernanceOptions.FuseSleepDuration = governanceProvider.FuseSleepDuration;
                GovernanceOptions.FailoverCount     = governanceProvider.FailoverCount;
                FailoverCountIsDefaultValue         = governanceProvider.FailoverCount == 0;
            }

            var governanceAttribute = governanceProvider as GovernanceAttribute;

            if (governanceAttribute?.FallBackType != null)
            {
                Type fallBackType;
                if (ReturnType == typeof(void))
                {
                    fallBackType = EngineContext.Current.TypeFinder.FindClassesOfType <IFallbackInvoker>()
                                   .FirstOrDefault(p => p == governanceAttribute.FallBackType);
                    if (fallBackType == null)
                    {
                        throw new LmsException($"未能找到{governanceAttribute.FallBackType.FullName}的实现类");
                    }
                }
                else
                {
                    fallBackType = typeof(IFallbackInvoker <>);
                    fallBackType = fallBackType.MakeGenericType(ReturnType);
                    if (!EngineContext.Current.TypeFinder.FindClassesOfType(fallBackType)
                        .Any(p => p == governanceAttribute.FallBackType))
                    {
                        throw new LmsException($"未能找到{governanceAttribute.FallBackType.FullName}的实现类");
                    }
                }

                var invokeMethod = fallBackType.GetMethods().First(p => p.Name == "Invoke");

                var fallbackMethodExcutor = ObjectMethodExecutor.Create(invokeMethod, fallBackType.GetTypeInfo(),
                                                                        ParameterDefaultValues.GetParameterDefaultValues(invokeMethod));
                FallBackExecutor = CreateFallBackExecutor(fallbackMethodExcutor, fallBackType);
            }

            GovernanceOptions.ProhibitExtranet = governanceAttribute?.ProhibitExtranet ?? false;
        }
Exemple #7
0
        private Function(Delegate @delegate, Type returnType, Type[] args,
                         FunctionMValueConstParser[] constParsers,
                         FunctionObjectParser[] objectParsers, FunctionStringParser[] stringParsers, FunctionTypeInfo[] typeInfos,
                         int requiredArgsCount)
        {
            this.@delegate         = @delegate;
            this.returnType        = returnType;
            this.args              = args;
            this.constParsers      = constParsers;
            this.objectParsers     = objectParsers;
            this.stringParsers     = stringParsers;
            this.typeInfos         = typeInfos;
            this.requiredArgsCount = requiredArgsCount;

            var parameterDefaultValues = ParameterDefaultValues
                                         .GetParameterDefaultValues(@delegate.Method);

            objectMethodExecutor = ObjectMethodExecutor.Create(
                @delegate.Method,
                @delegate.Target?.GetType().GetTypeInfo(),
                parameterDefaultValues);
            target = @delegate.Target;
        }