/// <summary>
        /// 创建Action执行器
        /// </summary>
        /// <param name="apiAction">action描述器</param>
        /// <returns></returns>
        private static IActionInvoker CreateActionInvoker(ApiActionDescriptor apiAction)
        {
            var dataType    = apiAction.Return.DataType.Type;
            var invokerType = typeof(MultiplexedActionInvoker <>).MakeGenericType(dataType);

            return(Lambda.CreateCtorFunc <ApiActionDescriptor, IActionInvoker>(invokerType)(apiAction));
        }
Esempio n. 2
0
        /// <summary>
        /// 创建Action执行器
        /// </summary>
        /// <param name="apiAction">action描述</param>
        /// <returns></returns>
        private static IActionInvoker CreateActionInvoker(ApiActionDescriptor apiAction)
        {
            var resultType  = apiAction.Return.DataType.Type;
            var invokerType = typeof(MultiplexedActionInvoker <>).MakeGenericType(resultType);

            return(invokerType.CreateInstance <IActionInvoker>(apiAction));
        }
Esempio n. 3
0
 /// <summary>
 /// 请求Api的上下文
 /// </summary>
 /// <param name="httpContext"></param>
 /// <param name="apiAction"></param>
 /// <param name="arguments"></param>
 /// <param name="properties"></param>
 protected ApiRequestContext(HttpContext httpContext, ApiActionDescriptor apiAction, object?[] arguments, DataCollection properties)
 {
     this.HttpContext = httpContext;
     this.ApiAction   = apiAction;
     this.Arguments   = arguments;
     this.Properties  = properties;
 }
Esempio n. 4
0
 /// <summary>
 /// 请求Api的上下文
 /// </summary>
 /// <param name="httpContext"></param>
 /// <param name="actionDescriptor"></param>
 /// <param name="arguments"></param>
 /// <param name="properties"></param>
 public ApiRequestContext(HttpContext httpContext, ApiActionDescriptor actionDescriptor, object?[] arguments, IDataCollection properties)
 {
     this.HttpContext      = httpContext;
     this.ActionDescriptor = actionDescriptor;
     this.Arguments        = arguments;
     this.Properties       = properties;
 }
Esempio n. 5
0
        /// <summary>
        /// 请求Api的上下文
        /// </summary>
        /// <param name="httpContext"></param>
        /// <param name="apiAction"></param>
        /// <param name="arguments"></param>
        /// <param name="userDatas"></param>
        /// <param name="cancellationTokens"></param>
        /// <exception cref="ArgumentNullException"></exception>
        protected ApiRequestContext(HttpContext httpContext, ApiActionDescriptor apiAction, object[] arguments, DataCollection userDatas, IList <CancellationToken> cancellationTokens)
        {
            this.HttpContext = httpContext ?? throw new ArgumentNullException(nameof(httpContext));
            this.ApiAction   = apiAction ?? throw new ArgumentNullException(nameof(apiAction));
            this.Arguments   = arguments ?? throw new ArgumentNullException(nameof(arguments));

            this.UserDatas          = userDatas;
            this.CancellationTokens = cancellationTokens;
        }
Esempio n. 6
0
        /// <summary>
        /// 请求Api的上下文
        /// </summary>
        /// <param name="httpContext"></param>
        /// <param name="apiAction"></param>
        /// <param name="arguments"></param>
        /// <param name="properties"></param>
        /// <param name="cancellationTokens"></param>
        protected ApiRequestContext(HttpContext httpContext, ApiActionDescriptor apiAction, object?[] arguments, DataCollection properties, IList <CancellationToken> cancellationTokens)
        {
            this.HttpContext = httpContext;
            this.ApiAction   = apiAction;
            this.Arguments   = arguments;

            this.Properties         = properties;
            this.CancellationTokens = cancellationTokens;
        }
Esempio n. 7
0
        /// <summary>
        /// 创建请求委托
        /// </summary>
        /// <param name="apiAction"></param>
        /// <returns></returns>
        private static InvokeDelegate <ApiRequestContext> BuildRequestHandler(ApiActionDescriptor apiAction)
        {
            var builder = new PipelineBuilder <ApiRequestContext>();

            // 参数验证特性验证和参数模型属性特性验证
            builder.Use(next => context =>
            {
                var validateProperty = context.HttpContext.Options.UseParameterPropertyValidate;
                foreach (var parameter in context.ApiAction.Parameters)
                {
                    var parameterValue = context.Arguments[parameter.Index];
                    ApiValidator.ValidateParameter(parameter, parameterValue, validateProperty);
                }
                return(next(context));
            });

            // action特性请求前执行
            foreach (var attr in apiAction.Attributes)
            {
                builder.Use(attr.OnRequestAsync);
            }

            // 参数特性请求前执行
            foreach (var parameter in apiAction.Parameters)
            {
                var index = parameter.Index;
                foreach (var attr in parameter.Attributes)
                {
                    builder.Use(async(context, next) =>
                    {
                        var ctx = new ApiParameterContext(context, index);
                        await attr.OnRequestAsync(ctx, next).ConfigureAwait(false);
                    });
                }
            }

            // Return特性请求前执行
            foreach (var @return in apiAction.Return.Attributes)
            {
                if (@return.Enable == true)
                {
                    builder.Use(@return.OnRequestAsync);
                }
            }

            // Filter请求前执行
            foreach (var filter in apiAction.FilterAttributes)
            {
                if (filter.Enable == true)
                {
                    builder.Use(filter.OnRequestAsync);
                }
            }

            return(builder.Build());
        }
Esempio n. 8
0
        /// <summary>
        /// 创建响应委托
        /// </summary>
        /// <param name="apiAction"></param>
        /// <returns></returns>
        private static InvokeDelegate <ApiResponseContext> BuildResponseHandler(ApiActionDescriptor apiAction)
        {
            var builder = new PipelineBuilder <ApiResponseContext>();

            // Return特性请求后执行
            foreach (var @return in apiAction.Return.Attributes)
            {
                if (@return.Enable == false)
                {
                    continue;
                }

                builder.Use(async(context, next) =>
                {
                    if (context.ResultStatus == ResultStatus.None)
                    {
                        await @return.OnResponseAsync(context, next).ConfigureAwait(false);
                    }
                    else
                    {
                        await next().ConfigureAwait(false);
                    }
                });
            }

            // 验证Result是否ok
            builder.Use(next => context =>
            {
                try
                {
                    ApiValidator.ValidateReturnValue(context.Result, context.HttpContext.Options.UseReturnValuePropertyValidate);
                }
                catch (Exception ex)
                {
                    context.Exception = ex;
                }
                return(next(context));
            });

            // Filter请求后执行
            foreach (var filter in apiAction.FilterAttributes)
            {
                if (filter.Enable == true)
                {
                    builder.Use(filter.OnResponseAsync);
                }
            }

            return(builder.Build());
        }
        /// <summary>
        /// 创建执行委托
        /// </summary>
        /// <param name="apiAction">action描述器</param>
        /// <returns></returns>
        public static Func <ApiRequestContext, Task <ApiResponseContext> > Build(ApiActionDescriptor apiAction)
        {
            var requestHandler  = BuildRequestHandler(apiAction);
            var responseHandler = BuildResponseHandler(apiAction);

            return(async request =>
            {
                await requestHandler(request).ConfigureAwait(false);

                var response = await HttpRequest.SendAsync(request).ConfigureAwait(false);
                await responseHandler(response).ConfigureAwait(false);

                return response;
            });
        }
Esempio n. 10
0
        /// <summary>
        /// 复合的ApiAction执行器
        /// </summary>
        /// <param name="apiAction">Api描述</param>
        public MultiplexedActionInvoker(ApiActionDescriptor apiAction)
        {
            var resultType = apiAction.Return.DataType.Type;

            this.isTaskDefinition = apiAction.Return.IsTaskDefinition;

            // (ApiActionDescriptor apiAction)
            var invokerType = typeof(ActionInvoker <>).MakeGenericType(resultType);

            this.actionInvoker = Lambda.CreateCtorFunc <ApiActionDescriptor, IActionInvoker>(invokerType)(apiAction);

            // (IActionInvoker invoker, ServiceContext context, object[] arguments)
            var actionTaskType = typeof(ActionTask <>).MakeGenericType(resultType);

            this.actionTaskCtor = Lambda.CreateCtorFunc <IActionInvoker, ServiceContext, object[], object>(actionTaskType);
        }
        /// <summary>
        /// 创建响应委托
        /// </summary>
        /// <param name="apiAction"></param>
        /// <returns></returns>
        private static InvokeDelegate <ApiResponseContext> BuildResponseHandler(ApiActionDescriptor apiAction)
        {
            var builder = new PipelineBuilder <ApiResponseContext>();

            // Return特性请求后执行
            foreach (var @return in apiAction.Return.Attributes)
            {
                builder.Use(next => async context =>
                {
                    if (context.ResultStatus == ResultStatus.None)
                    {
                        try
                        {
                            await @return.OnResponseAsync(context).ConfigureAwait(false);
                        }
                        catch (Exception ex)
                        {
                            context.Exception = ex;
                        }
                    }
                    await next(context).ConfigureAwait(false);
                });
            }

            // 验证Result是否ok
            builder.Use(next => context =>
            {
                if (context.ApiAction.Return.DataType.IsRawType == true)
                {
                    return(next(context));
                }

                if (context.ResultStatus != ResultStatus.HasResult)
                {
                    return(next(context));
                }

                if (context.HttpContext.Options.UseReturnValuePropertyValidate == false)
                {
                    return(next(context));
                }

                try
                {
                    DataValidator.ValidateReturnValue(context.Result);
                }
                catch (Exception ex)
                {
                    context.Exception = ex;
                }
                return(next(context));
            });

            // Filter请求后执行
            foreach (var filter in apiAction.FilterAttributes)
            {
                builder.Use(next => async context =>
                {
                    await filter.OnResponseAsync(context).ConfigureAwait(false);
                    await next(context).ConfigureAwait(false);
                });
            }

            return(builder.Build());
        }
Esempio n. 12
0
 /// <summary>
 /// 请求Api的上下文
 /// </summary>
 /// <param name="httpContext"></param>
 /// <param name="apiAction"></param>
 /// <param name="arguments"></param>
 /// <exception cref="ArgumentNullException"></exception>
 public ApiRequestContext(HttpContext httpContext, ApiActionDescriptor apiAction, object[] arguments)
     : this(httpContext, apiAction, arguments, new DataCollection(), new List <CancellationToken>())
 {
 }
 /// <summary>
 /// 复合的ApiAction执行器
 /// 支持Task和ITask返回声明
 /// </summary>
 /// <param name="apiAction">Api描述</param>
 public MultiplexedActionInvoker(ApiActionDescriptor apiAction)
 {
     this.isTaskResult  = apiAction.Return.ReturnType.IsInheritFrom <Task>();
     this.actionInvoker = new ActionInvoker <TResult>(apiAction);
 }
Esempio n. 14
0
        /// <summary>
        /// 创建action执行器
        /// </summary>
        /// <param name="method">接口的方法</param>
        /// <returns></returns>
        private static IActionInvoker CreateActionInvoker(Method method)
        {
            var apiAction = new ApiActionDescriptor(method.MethodInfo, method.InterfaceType);

            return(new MultiplexedActionInvoker(apiAction));
        }
Esempio n. 15
0
 /// <summary>
 /// 请求Api的上下文
 /// </summary>
 /// <param name="httpContext"></param>
 /// <param name="apiAction"></param>
 /// <param name="arguments"></param>
 public ApiRequestContext(HttpContext httpContext, ApiActionDescriptor apiAction, object?[] arguments)
     : this(httpContext, apiAction, arguments, new DataCollection())
 {
 }
Esempio n. 16
0
 /// <summary>
 /// 上下文执行器
 /// </summary>
 /// <param name="apiAction"></param>
 public ActionInvoker(ApiActionDescriptor apiAction)
 {
     this.ApiAction      = apiAction;
     this.requestHandler = RequestDelegateBuilder.Build(apiAction);
 }
Esempio n. 17
0
 /// <summary>
 /// 上下文执行器
 /// </summary>
 /// <param name="apiAction"></param>
 public ActionInvoker(ApiActionDescriptor apiAction)
 {
     this.ApiAction = apiAction;
 }