コード例 #1
0
        public async Task <T> ConvertRequest <T>(HttpRequest request) where T : BaseRequest, new()
        {
            T result = (T)null;

            //TODO: CALL THE TYPE BASED VERSION?
            if (request.ContentLength.GetValueOrDefault() > 0)
            {
                await using var ms = new MemoryStream();
                await request.Body.CopyToAsync(ms);

                var message = ms.ToArray();
                result = await _serializer.DeserializeRequest <T>(message);
            }

            if (request.Query.Any())
            {
                result ??= new T();
                PropertyInfo[] properties = typeof(T).GetProperties();
                foreach (PropertyInfo property in properties)
                {
                    var qv  = request.Query[property.Name];
                    var val = Convert.ChangeType("", property.PropertyType);
                    property.SetValue(result, val);
                }
            }

            return(result ?? new T());
        }
コード例 #2
0
        public async Task <BaseRequest> ConvertRequest(Type type, HttpRequestMessage request)
        {
            HttpDataSerializerOptions options = CreateOptionsFromHeaders(request.Headers);

            byte[] contents = await request.Content.ReadAsByteArrayAsync();

            if (ShouldValidateSchema(options))
            {
                var schemaErrors = await _serializationSchema.ValidateSchema(contents, type, options);

                if (schemaErrors.Any())
                {
                    throw new SchemaValidationException($"Cannot validate payload as type of {type.FullName}", schemaErrors);
                }

                BaseRequest validResult = await _serializer.DeserializeRequest(type, contents, options);

                return(validResult);
            }

            BaseRequest result = await _serializer.DeserializeRequest(type, contents, options);

            return(result);
        }