Пример #1
0
 /// <summary>
 /// 执行api
 /// </summary>
 /// <param name="context">上下文</param>
 /// <returns></returns>
 public object Execute(ApiActionContext context)
 {
     if (ReturnTaskType.IsGenericType && ReturnTaskType.GetGenericTypeDefinition() == typeof(Task <>))
     {
         return(this.ExecuteAsync(context).CastResult(this.ReturnDataType));
     }
     else
     {
         return(this.ExecuteAsync(context).Result);
     }
 }
Пример #2
0
 /// <summary>
 /// 执行前
 /// </summary>
 /// <param name="context">上下文</param>
 /// <returns></returns>
 public override Task BeforeRequestAsync(ApiActionContext context)
 {
     if (context is HttpApiActionContext httpApiActionContext)
     {
         httpApiActionContext.RequestMessage.Method = this.Method;
         var routes = context.RouteAttributes.Where((x, i) => string.IsNullOrEmpty(this.Path) || i < 1)
                      .Select(x => x.Template.Trim('/'));
         httpApiActionContext.RequestMessage.RequestUri = new Uri(context.HttpApiClient.RequestHost,
                                                                  string.Join("/", routes) + '/' + this.Path.Trim('/'));
     }
     return(Task.CompletedTask);
 }
Пример #3
0
        /// <summary>
        /// 异步执行api
        /// </summary>
        /// <param name="context">上下文</param>
        /// <returns></returns>
        private async Task <object> ExecuteAsync(ApiActionContext context)
        {
            foreach (var methodAttribute in context.ApiActionDescriptor.Attributes)
            {
                await methodAttribute.BeforeRequestAsync(context);
            }

            foreach (var parameter in context.ApiActionDescriptor.Parameters)
            {
                foreach (var parameterAttribute in parameter.Attributes)
                {
                    await parameterAttribute.BeforeRequestAsync(context, parameter);
                }
            }

            foreach (var filter in context.ApiActionFilterAttributes)
            {
                await filter.OnBeginRequestAsync(context);
            }

            await context.HttpApiClient.SendAsync(context);

            foreach (var filter in context.ApiActionFilterAttributes)
            {
                await filter.OnEndRequestAsync(context);
            }

            if (context.ApiReturnAttribute != null)
            {
                return(await context.ApiReturnAttribute.GetTaskResult(context));
            }

            foreach (var attr in AppDomain.CurrentDomain.GetExcutingAssembiles().SelectMany(x => x.GetTypes())
                     .Where(x => x.BaseType == typeof(ApiReturnAttribute)).Select(x => Activator.CreateInstance(x) as ApiReturnAttribute))
            {
                if (attr == null)
                {
                    continue;
                }
                var result = await attr.GetTaskResult(context);

                if (result != null)
                {
                    return(result);
                }
            }

            var message = $"不支持的类型{context.ApiActionDescriptor.ReturnDataType}的解析";

            throw new NotSupportedException(message);
        }
Пример #4
0
        /// <summary>
        /// 异步执行api
        /// </summary>
        /// <param name="context">上下文</param>
        /// <returns></returns>
        private async Task <object> ExecuteAsync(ApiActionContext context)
        {
            foreach (var methodAttribute in context.ApiActionDescriptor.Attributes)
            {
                await methodAttribute.BeforeRequestAsync(context);
            }

            foreach (var parameter in context.ApiActionDescriptor.Parameters)
            {
                foreach (var parameterAttribute in parameter.Attributes)
                {
                    await parameterAttribute.BeforeRequestAsync(context, parameter);
                }
            }

            foreach (var filter in context.ApiActionFilterAttributes)
            {
                await filter.OnBeginRequestAsync(context);
            }

            await context.HttpApiClient.SendAsync(context);

            foreach (var filter in context.ApiActionFilterAttributes)
            {
                await filter.OnEndRequestAsync(context);
            }

            if (context.ApiReturnAttribute != null)
            {
                return(await context.ApiReturnAttribute.GetTaskResult(context));
            }

            foreach (var attr in ApiReturnAttributes)
            {
                if (attr == null)
                {
                    continue;
                }
                var result = await attr.GetTaskResult(context);

                if (result != null)
                {
                    return(result);
                }
            }

            var message = $"不支持的类型{context.ApiActionDescriptor.ReturnDataType}的解析";

            throw new NotSupportedException(message);
        }
Пример #5
0
 /// <summary>
 /// 获取异步结果
 /// </summary>
 /// <param name="context">上下文</param>
 /// <returns></returns>
 public abstract Task <object> GetTaskResult(ApiActionContext context);
Пример #6
0
 /// <summary>
 /// 请求完成之后
 /// </summary>
 /// <param name="context">上下文</param>
 /// <returns></returns>
 public virtual Task OnEndRequestAsync(ApiActionContext context)
 {
     return(Task.CompletedTask);
 }
Пример #7
0
 /// <summary>
 /// 执行前
 /// </summary>
 /// <param name="context">上下文</param>
 /// <returns></returns>
 public abstract Task BeforeRequestAsync(ApiActionContext context);
Пример #8
0
 /// <summary>
 /// http请求之前
 /// </summary>
 /// <param name="context">上下文</param>
 /// <param name="parameter">特性关联的参数</param>
 /// <returns></returns>
 public abstract Task BeforeRequestAsync(ApiActionContext context, ApiParameterDescriptor parameter);