예제 #1
0
        public void ExposeType(ICurrentApiInformation currentApi,
                               Type type,
                               ServiceActivationMethod serviceActivationMethod,
                               Func <RequestExecutionContext, object> activationFunc,
                               string name,
                               List <IEndPointMethodAuthorization> authorizations, Func <MethodInfo, bool> methodFilter,
                               string obsoleteMessage)
        {
            methodFilter ??= DefaultFilterMethod;
            var classAttributes = type.GetCustomAttributes <Attribute>().ToList();

            foreach (var methodInfo in type.GetMethods().Where(methodFilter))
            {
                ExposeMethod(currentApi, type, serviceActivationMethod, activationFunc, classAttributes, name, authorizations, obsoleteMessage, methodInfo);
            }
        }
예제 #2
0
        protected virtual void ExposeMethod(ICurrentApiInformation currentApi, Type type,
                                            ServiceActivationMethod serviceActivationMethod,
                                            Func <RequestExecutionContext, object> activationFunc,
                                            List <Attribute> classAttributes, string name, List <IEndPointMethodAuthorization> authorizations,
                                            string obsoleteMessage, MethodInfo methodInfo)
        {
            var methodAttributes = methodInfo.GetCustomAttributes <Attribute>().ToList();

            // skip methods that have IgnoreMethodAttribute
            if (methodAttributes.Any(a => a is IgnoreMethodAttribute))
            {
                return;
            }

            var pathAttributes = methodAttributes.Where(a => a is IPathAttribute).ToArray();

            if (pathAttributes.Length > 0)
            {
                foreach (var pathAttribute in pathAttributes)
                {
                    foreach (var configuration in CreateEndPointMethodConfiguration(currentApi, type,
                                                                                    serviceActivationMethod, activationFunc, classAttributes, name,
                                                                                    authorizations, obsoleteMessage, methodInfo, methodAttributes, pathAttribute as IPathAttribute))
                    {
                        var endPointMethodHandler =
                            CreateEndPointMethodHandler(currentApi, configuration);

                        _handlers.Add(endPointMethodHandler);
                    }
                }
            }
            else
            {
                foreach (var configuration in CreateEndPointMethodConfiguration(currentApi, type, serviceActivationMethod, activationFunc, classAttributes, name,
                                                                                authorizations, obsoleteMessage, methodInfo, methodAttributes, null))
                {
                    var endPointMethodHandler =
                        CreateEndPointMethodHandler(currentApi, configuration);

                    _handlers.Add(endPointMethodHandler);
                }
            }
        }
예제 #3
0
        public CurrentApiInformation(
            ImmutableLinkedList <Func <IEndPointMethodConfigurationReadOnly, IEnumerable <IEndPointMethodAuthorization> > > authorizations,
            ImmutableLinkedList <Func <IEndPointMethodConfigurationReadOnly, Func <RequestExecutionContext, IRequestFilter> > > filters,
            ImmutableLinkedList <Func <Type, IEnumerable <string> > > prefixes,
            ImmutableLinkedList <Func <MethodInfo, bool> > methodFilters,
            bool supportResponseCompression,
            ExposeDefaultMethod defaultMethod,
            ServiceActivationMethod serviceActivationMethod,
            IServiceProvider serviceProvider,
            IConfigurationManager configurationMethods,
            ImmutableLinkedList <IResponseHeader> headers)

        {
            Authorizations             = authorizations;
            Filters                    = filters;
            Prefixes                   = prefixes;
            MethodFilters              = methodFilters;
            SupportResponseCompression = supportResponseCompression;
            DefaultMethod              = defaultMethod;
            ServiceActivationMethod    = serviceActivationMethod;
            ServiceProvider            = serviceProvider;
            ConfigurationMethods       = configurationMethods;
            Headers                    = headers;
        }
예제 #4
0
        private IEnumerable <EndPointMethodConfiguration> CreateEndPointMethodConfiguration(
            ICurrentApiInformation currentApi,
            Type type,
            ServiceActivationMethod serviceActivationMethod,
            Func <RequestExecutionContext, object> activationFunc,
            List <Attribute> classAttributes,
            string name,
            List <IEndPointMethodAuthorization> authorizations,
            string obsoleteMessage,
            MethodInfo methodInfo, List <Attribute> methodAttributes, IPathAttribute pathAttribute)
        {
            string methodPath;
            string methodVerb;
            bool   methodHasBody;

            (methodPath, methodVerb, methodHasBody) = GenerateMethodPath(currentApi, type, name, methodInfo, classAttributes, methodAttributes, pathAttribute);

            if (activationFunc == null)
            {
                activationFunc = GenerateActivation(currentApi, type, classAttributes, name, methodInfo, methodAttributes);
            }

            foreach (var routeInformation in GenerateRouteInformationList(methodPath, methodVerb, methodHasBody, currentApi, type, name, methodInfo, methodAttributes))
            {
                var configuration = new EndPointMethodConfiguration(routeInformation, activationFunc, new MethodInvokeInformation {
                    MethodToInvoke = methodInfo
                }, methodInfo.ReturnType);

                AssignDefaultValues(configuration, pathAttribute);

                var methodParameters = GenerateMethodParameters(type, methodInfo, routeInformation);

                configuration.Parameters.AddRange(methodParameters);

                configuration.Parameters.AddRange(AddInstancePropertyBindingParameters(configuration, type));

                var rawAttribute = (IRawContentAttribute)methodAttributes.FirstOrDefault(a => a is IRawContentAttribute);

                if (rawAttribute != null)
                {
                    configuration.RawContentType     = rawAttribute.ContentType;
                    configuration.RawContentEncoding = rawAttribute.ContentEncoding;
                }
                else if (string.IsNullOrEmpty(configuration.RawContentType))
                {
                    var returnType = methodInfo.ReturnType;

                    if (returnType.IsConstructedGenericType &&
                        (returnType.GetGenericTypeDefinition() == typeof(Task <>) ||
                         returnType.GetGenericTypeDefinition() == typeof(ValueTask <>)))
                    {
                        returnType = returnType.GenericTypeArguments[0];
                    }

                    if (_exposeConfigurations.TypeWrapSelector(returnType))
                    {
                        configuration.WrappedType = _wrappedResultTypeCreator.GetTypeWrapper(returnType);
                    }
                }

                var headerAttributes = classAttributes.Where(a => a is ResponseHeaderAttribute).ToList();
                headerAttributes.AddRange(methodAttributes.Where(a => a is ResponseHeaderAttribute));

                if (headerAttributes.Count > 0 ||
                    currentApi.Headers != ImmutableLinkedList <IResponseHeader> .Empty)
                {
                    var headers = new List <IResponseHeader>();

                    headers.AddRange(currentApi.Headers);

                    foreach (ResponseHeaderAttribute headerAttribute in headerAttributes)
                    {
                        headers.Add(new ResponseHeader.ResponseHeader(headerAttribute.Name, headerAttribute.Value));
                    }

                    configuration.ResponseHeaders = headers;
                }

                Func <IEndPointMethodConfigurationReadOnly, IEnumerable <IEndPointMethodAuthorization> >[]
                authorizationFunc = null;

                if (authorizations != null)
                {
                    authorizationFunc =
                        new Func <IEndPointMethodConfigurationReadOnly, IEnumerable <IEndPointMethodAuthorization> >[]
                    {
                        config => authorizations
                    };
                }

                ApplyAuthorizations(currentApi, authorizationFunc, configuration, classAttributes, methodAttributes);
                ApplyFilters(currentApi, GetFilterList(currentApi, configuration, classAttributes, methodAttributes), configuration);

                if (_supportCompression)
                {
                    configuration.SupportsCompression = _compressionSelectorService.ShouldCompressResult(configuration);
                }

                var returnTypeAttribute = (ReturnsTypeAttribute)methodAttributes.FirstOrDefault(a => a is ReturnsTypeAttribute);

                configuration.DocumentationReturnType = returnTypeAttribute?.ReturnType;

                yield return(configuration);
            }
        }