/// <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); if (hostAttribute == null) { hostAttribute = invocation.Proxy.GetType().GetCustomAttribute <HttpHostAttribute>(); } if (hostAttribute == null) { throw new HttpRequestException("未指定HttpHostAttribute"); } var returnAttribute = CastleContext.GetAttributeFromMethodOrInterface <ApiReturnAttribute>(method, true); if (returnAttribute == null) { returnAttribute = new DefaultReturnAttribute(); } 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, ApiReturnAttribute = returnAttribute, ApiActionFilterAttributes = filterAttributes, ApiActionDescriptor = CastleContext.GetActionDescriptor(invocation) }); }
/// <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); }
/// <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, 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); }