public override Task WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding)
        {
            if (context == null)
            {
                throw Error.ArgumentNull("context");
            }

            Type type = context.ObjectType;

            if (type == null)
            {
                throw Error.ArgumentNull("type");
            }
            type = TypeHelper.GetTaskInnerTypeOrSelf(type);

            HttpRequest request = context.HttpContext.Request;

            if (request == null)
            {
                throw Error.InvalidOperation(SRResources.WriteToStreamAsyncMustHaveRequest);
            }

            try
            {
                // TODO: We should remove this and make other serializers call using Async?
                var body = request.HttpContext.Features.Get <Http.Features.IHttpBodyControlFeature>();
                if (body != null)
                {
                    body.AllowSynchronousIO = true;
                }

                HttpResponse         response    = context.HttpContext.Response;
                Uri                  baseAddress = GetBaseAddressInternal(request);
                MediaTypeHeaderValue contentType = GetContentType(response.Headers[HeaderNames.ContentType].FirstOrDefault());

                ODataSerializerProvider serializerProvider = request.GetSubServiceProvider().GetRequiredService <ODataSerializerProvider>();

                ODataOutputFormatterHelper.WriteToStream(
                    type,
                    context.Object,
                    request.GetModel(),
                    request.GetODataResponseVersion(),
                    baseAddress,
                    contentType,
                    request,
                    request.Headers,
                    serializerProvider);

                return(Task.CompletedTask);
            }
            catch (Exception ex)
            {
                return(Task.FromException(ex));
            }
        }
Exemplo n.º 2
0
        public override Task WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding)
        {
            if (context == null)
            {
                throw Error.ArgumentNull("context");
            }

            Type type = context.ObjectType;

            if (type == null)
            {
                throw Error.ArgumentNull("type");
            }
            type = TypeHelper.GetTaskInnerTypeOrSelf(type);

            HttpRequest request = context.HttpContext.Request;

            if (request == null)
            {
                throw Error.InvalidOperation(SRResources.WriteToStreamAsyncMustHaveRequest);
            }

            HttpResponse response = context.HttpContext.Response;

            if (typeof(Stream).IsAssignableFrom(type))
            {
                // Ideally, it should go into the "ODataRawValueSerializer"
                // However, OData lib doesn't provide the method to overwrite/copyto stream
                // So, Here's the workaround
                Stream objStream = context.Object as Stream;
                return(CopyStreamAsync(objStream, response));
            }

            Uri baseAddress = GetBaseAddressInternal(request);
            MediaTypeHeaderValue contentType = GetContentType(response.Headers[HeaderNames.ContentType].FirstOrDefault());

            ODataSerializerProvider serializerProvider = request.GetSubServiceProvider().GetRequiredService <ODataSerializerProvider>();

            return(ODataOutputFormatterHelper.WriteToStreamAsync(
                       type,
                       context.Object,
                       request.GetModel(),
                       request.GetODataVersion(),
                       baseAddress,
                       contentType,
                       request,
                       request.Headers,
                       serializerProvider));
        }
        public override Task WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding)
        {
            if (context == null)
            {
                throw Error.ArgumentNull("context");
            }

            Type type = context.ObjectType;

            if (type == null)
            {
                throw Error.ArgumentNull("type");
            }
            type = TypeHelper.GetTaskInnerTypeOrSelf(type);

            HttpRequest request = context.HttpContext.Request;

            if (request == null)
            {
                throw Error.InvalidOperation(SRResources.WriteToStreamAsyncMustHaveRequest);
            }

            HttpResponse         response    = context.HttpContext.Response;
            Uri                  baseAddress = GetBaseAddressInternal(request);
            MediaTypeHeaderValue contentType = GetContentType(response.Headers[HeaderNames.ContentType].FirstOrDefault());

            ODataSerializerProvider serializerProvider = request.GetSubServiceProvider().GetRequiredService <ODataSerializerProvider>();

            return(ODataOutputFormatterHelper.WriteToStreamAsync(
                       type,
                       context.Object,
                       request.GetModel(),
                       request.GetODataResponseVersion(),
                       baseAddress,
                       contentType,
                       request,
                       request.Headers,
                       serializerProvider));
        }