Example #1
0
        /// <summary>
        /// 从拦截内容获得
        /// </summary>
        /// <param name="invocation">拦截内容</param>
        /// <returns></returns>
        private static CastleContext GetContextNoCache(IInvocation invocation)
        {
            var method        = invocation.Method;
            var hostAttribute = CastleContext.GetAttributeFromMethodOrInterface <HttpHostAttribute>(method, false) ??
                                invocation.Proxy?.GetType().GetCustomAttribute <HttpHostAttribute>() ??
                                throw new HttpRequestException("未指定HttpHostAttribute");

            var routeAttributes = CastleContext.GetAttributesFromMethodAndInterface <RouteAttribute>(method, false) ??
                                  new RouteAttribute[] { };

            var returnAttribute = CastleContext.GetAttributeFromMethodOrInterface <ApiReturnAttribute>(method, true);

            var methodFilters    = method.GetCustomAttributes <ApiActionFilterAttribute>(true);
            var interfaceFilters = method.DeclaringType.GetCustomAttributes <ApiActionFilterAttribute>(true);
            var filterAttributes = methodFilters.Concat(interfaceFilters).Distinct(new ApiActionFilterAttributeComparer()).ToArray();

            return(new CastleContext
            {
                HostAttribute = hostAttribute,
                RouteAttributes = routeAttributes,
                ApiReturnAttribute = returnAttribute,
                ApiActionFilterAttributes = filterAttributes,
                ApiActionDescriptor = CastleContext.GetActionDescriptor(invocation)
            });
        }
Example #2
0
        /// <summary>
        /// 方法拦截
        /// </summary>
        /// <param name="invocation">拦截内容</param>
        void IInterceptor.Intercept(IInvocation invocation)
        {
            var context       = CastleContext.From(invocation);
            var actionContext = new ApiActionContext
            {
                HttpApiClient             = this,
                RequestMessage            = new HttpRequestMessage(),
                HostAttribute             = context.HostAttribute,
                RouteAttributes           = context.RouteAttributes,
                ApiReturnAttribute        = context.ApiReturnAttribute,
                ApiActionFilterAttributes = context.ApiActionFilterAttributes,
                ApiActionDescriptor       = context.ApiActionDescriptor.Clone() as ApiActionDescriptor
            };

            var parameters = actionContext.ApiActionDescriptor.Parameters;

            for (var i = 0; i < parameters.Length; i++)
            {
                parameters[i].Value = invocation.Arguments[i];
            }

            var apiAction = context.ApiActionDescriptor;

            invocation.ReturnValue = apiAction.Execute(actionContext);
        }
Example #3
0
        /// <summary>
        /// 生成ApiParameterDescriptor
        /// </summary>
        /// <param name="parameter">参数信息</param>
        /// <param name="index">参数索引</param>
        /// <returns></returns>
        private static ApiParameterDescriptor GetParameterDescriptor(ParameterInfo parameter, int index)
        {
            var parameterDescriptor = new ApiParameterDescriptor
            {
                Name          = parameter.Name,
                Index         = index,
                ParameterType = parameter.ParameterType,
                IsSimpleType  = CastleContext.IsSimple(parameter.ParameterType),
                Attributes    = parameter.GetCustomAttributes <ApiParameterAttribute>(true).ToArray()
            };

            if (typeof(HttpContent).IsAssignableFrom(parameter.ParameterType))
            {
                parameterDescriptor.Attributes = new[] { new HttpContentAttribute() };
            }
            else if (parameterDescriptor.Attributes.Length == 0)
            {
                parameterDescriptor.Attributes = new[] { new PathQueryAttribute() };
            }
            return(parameterDescriptor);
        }