Exemplo n.º 1
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());
        }
Exemplo n.º 2
0
        /// <summary>
        /// 序列化参数值为Xml
        /// </summary>
        /// <param name="context"></param>
        /// <param name="encoding">xml的编码</param>
        /// <returns></returns>
        public static string?SerializeToXml(this ApiParameterContext context, Encoding?encoding)
        {
            var options = context.HttpContext.HttpApiOptions.XmlSerializeOptions;

            if (encoding != null && encoding.Equals(options.Encoding) == false)
            {
                options          = options.Clone();
                options.Encoding = encoding;
            }

            return(XmlSerializer.Serialize(context.ParameterValue, options));
        }
Exemplo n.º 3
0
        /// <summary>
        /// 序列化参数值为指定编码的Json
        /// </summary>
        /// <param name="context"></param>
        /// <param name="encoding">编码</param>
        /// <returns></returns>
        public static byte[] SerializeToJson(this ApiParameterContext context, Encoding encoding)
        {
            using var bufferWriter = new RecyclableBufferWriter <byte>();
            context.SerializeToJson(bufferWriter);

            if (Encoding.UTF8.Equals(encoding) == true)
            {
                return(bufferWriter.WrittenSpan.ToArray());
            }
            else
            {
                var utf8Json = bufferWriter.WrittenSegment;
                return(Encoding.Convert(Encoding.UTF8, encoding, utf8Json.Array, utf8Json.Offset, utf8Json.Count));
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// 处理请求上下文
        /// </summary>
        /// <returns></returns>
        private static async Task HandleRequestAsync(ApiRequestContext context)
        {
            // 参数验证
            var validateProperty = context.HttpContext.HttpApiOptions.UseParameterPropertyValidate;

            foreach (var parameter in context.ApiAction.Parameters)
            {
                var parameterValue = context.Arguments[parameter.Index];
                DataValidator.ValidateParameter(parameter, parameterValue, validateProperty);
            }

            // action特性请求前执行
            foreach (var attr in context.ApiAction.Attributes)
            {
                await attr.OnRequestAsync(context).ConfigureAwait(false);
            }

            // 参数特性请求前执行
            foreach (var parameter in context.ApiAction.Parameters)
            {
                var ctx = new ApiParameterContext(context, parameter);
                foreach (var attr in parameter.Attributes)
                {
                    await attr.OnRequestAsync(ctx).ConfigureAwait(false);
                }
            }

            // Return特性请求前执行
            foreach (var @return in context.ApiAction.Return.Attributes)
            {
                await @return.OnRequestAsync(context).ConfigureAwait(false);
            }

            // GlobalFilter请求前执行
            foreach (var filter in context.HttpContext.HttpApiOptions.GlobalFilters)
            {
                await filter.OnRequestAsync(context).ConfigureAwait(false);
            }

            // Filter请求前执行
            foreach (var filter in context.ApiAction.FilterAttributes)
            {
                await filter.OnRequestAsync(context).ConfigureAwait(false);
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// 创建请求上下文处理委托
        /// </summary>
        /// <returns></returns>
        private static InvokeDelegate <ApiRequestContext> BuildRequestHandler()
        {
            var builder = new PipelineBuilder <ApiRequestContext>();

            // 参数验证
            builder.Use(next => context =>
            {
                var validateProperty = context.HttpContext.HttpApiOptions.UseParameterPropertyValidate;
                foreach (var parameter in context.ApiAction.Parameters)
                {
                    var parameterValue = context.Arguments[parameter.Index];
                    DataValidator.ValidateParameter(parameter, parameterValue, validateProperty);
                }
                return(next(context));
            });

            // action特性请求前执行
            builder.Use(next => async context =>
            {
                foreach (var attr in context.ApiAction.Attributes)
                {
                    await attr.OnRequestAsync(context).ConfigureAwait(false);
                }
                await next(context).ConfigureAwait(false);
            });

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

            // Return特性请求前执行
            builder.Use(next => async context =>
            {
                foreach (var @return in context.ApiAction.Return.Attributes)
                {
                    await @return.OnRequestAsync(context).ConfigureAwait(false);
                }
                await next(context).ConfigureAwait(false);
            });

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

            return(builder.Build());
        }
Exemplo n.º 6
0
        /// <summary>
        /// 序列化参数值为键值对
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public static IList <KeyValue> SerializeToKeyValues(this ApiParameterContext context)
        {
            var options = context.HttpContext.HttpApiOptions.KeyValueSerializeOptions;

            return(KeyValueSerializer.Serialize(context.ParameterName, context.ParameterValue, options));
        }
Exemplo n.º 7
0
        /// <summary>
        /// 序列化参数值为utf8编码的Json
        /// </summary>
        /// <param name="context"></param>
        /// <param name="bufferWriter">buffer写入器</param>
        public static void SerializeToJson(this ApiParameterContext context, IBufferWriter <byte> bufferWriter)
        {
            var options = context.HttpContext.HttpApiOptions.JsonSerializeOptions;

            JsonBufferSerializer.Serialize(bufferWriter, context.ParameterValue, options);
        }
Exemplo n.º 8
0
 /// <summary>
 /// 序列化参数值为utf8编码的Json
 /// </summary>
 /// <param name="context"></param>
 /// <returns></returns>
 public static byte[] SerializeToJson(this ApiParameterContext context)
 {
     return(context.SerializeToJson(Encoding.UTF8));
 }