コード例 #1
0
        public async Task WriteAsync(OutputFormatterWriteContext context)
        {
            ArgumentGuard.NotNull(context, nameof(context));

            var response = context.HttpContext.Response;

            response.ContentType = _serializer.ContentType;

            await using var writer = context.WriterFactory(response.Body, Encoding.UTF8);
            string responseContent;

            try
            {
                responseContent = SerializeResponse(context.Object, (HttpStatusCode)response.StatusCode);
            }
            catch (Exception exception)
            {
                var errorDocument = _exceptionHandler.HandleException(exception);
                responseContent = _serializer.Serialize(errorDocument);

                response.StatusCode = (int)errorDocument.GetErrorStatusCode();
            }

            var url = context.HttpContext.Request.GetEncodedUrl();

            _traceWriter.LogMessage(() => $"Sending {response.StatusCode} response for request at '{url}' with body: <<{responseContent}>>");

            await writer.WriteAsync(responseContent);

            await writer.FlushAsync();
        }
コード例 #2
0
        public async Task WriteAsync(OutputFormatterWriteContext context)
        {
            ArgumentGuard.NotNull(context, nameof(context));

            HttpResponse response = context.HttpContext.Response;

            response.ContentType = _serializer.ContentType;

            await using TextWriter writer = context.WriterFactory(response.Body, Encoding.UTF8);
            string responseContent;

            try
            {
                responseContent = SerializeResponse(context.Object, (HttpStatusCode)response.StatusCode);
            }
#pragma warning disable AV1210 // Catch a specific exception instead of Exception, SystemException or ApplicationException
            catch (Exception exception)
#pragma warning restore AV1210 // Catch a specific exception instead of Exception, SystemException or ApplicationException
            {
                ErrorDocument errorDocument = _exceptionHandler.HandleException(exception);
                responseContent = _serializer.Serialize(errorDocument);

                response.StatusCode = (int)errorDocument.GetErrorStatusCode();
            }

            string url = context.HttpContext.Request.GetEncodedUrl();
            _traceWriter.LogMessage(() => $"Sending {response.StatusCode} response for request at '{url}' with body: <<{responseContent}>>");

            await writer.WriteAsync(responseContent);

            await writer.FlushAsync();
        }
コード例 #3
0
        public async Task <InputFormatterResult> ReadAsync(InputFormatterContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var request = context.HttpContext.Request;

            if (request.ContentLength == 0)
            {
                return(await InputFormatterResult.SuccessAsync(null));
            }

            string body = await GetRequestBody(context.HttpContext.Request.Body);

            string url = context.HttpContext.Request.GetEncodedUrl();

            _traceWriter.LogMessage(() => $"Received request at '{url}' with body: <<{body}>>");

            object model;

            try
            {
                model = _deserializer.Deserialize(body);
            }
            catch (InvalidRequestBodyException exception)
            {
                exception.SetRequestBody(body);
                throw;
            }
            catch (Exception exception)
            {
                throw new InvalidRequestBodyException(null, null, body, exception);
            }

            ValidatePatchRequestIncludesId(context, model, body);

            ValidateIncomingResourceType(context, model);

            return(await InputFormatterResult.SuccessAsync(model));
        }
コード例 #4
0
        public async Task <InputFormatterResult> ReadAsync(InputFormatterContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            string body = await GetRequestBodyAsync(context.HttpContext.Request.Body);

            string url = context.HttpContext.Request.GetEncodedUrl();

            _traceWriter.LogMessage(() => $"Received request at '{url}' with body: <<{body}>>");

            object model = null;

            if (!string.IsNullOrWhiteSpace(body))
            {
                try
                {
                    model = _deserializer.Deserialize(body);
                }
                catch (JsonApiSerializationException exception)
                {
                    throw ToInvalidRequestBodyException(exception, body);
                }
                catch (Exception exception)
                {
                    throw new InvalidRequestBodyException(null, null, body, exception);
                }
            }

            if (_request.Kind == EndpointKind.AtomicOperations)
            {
                AssertHasRequestBody(model, body);
            }
            else if (RequiresRequestBody(context.HttpContext.Request.Method))
            {
                ValidateRequestBody(model, body, context.HttpContext.Request);
            }

            return(await InputFormatterResult.SuccessAsync(model));
        }
コード例 #5
0
        public async Task WriteAsync(OutputFormatterWriteContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var response = context.HttpContext.Response;

            await using var writer = context.WriterFactory(response.Body, Encoding.UTF8);
            string responseContent;

            if (_serializer == null)
            {
                responseContent = JsonConvert.SerializeObject(context.Object);
            }
            else
            {
                response.ContentType = HeaderConstants.MediaType;
                try
                {
                    responseContent = SerializeResponse(context.Object, (HttpStatusCode)response.StatusCode);
                }
                catch (Exception exception)
                {
                    var errorDocument = _exceptionHandler.HandleException(exception);
                    responseContent = _serializer.Serialize(errorDocument);

                    response.StatusCode = (int)errorDocument.GetErrorStatusCode();
                }
            }

            var url = context.HttpContext.Request.GetEncodedUrl();

            _traceWriter.LogMessage(() => $"Sending {response.StatusCode} response for request at '{url}' with body: <<{responseContent}>>");

            await writer.WriteAsync(responseContent);

            await writer.FlushAsync();
        }