public static async ValueTask <OeOperationMessage> CreateAsync(IEdmModel edmModel, Uri baseUri, ODataBatchReader reader, IServiceProvider?serviceProvider)
        {
            ODataBatchOperationRequestMessage batchRequest = await reader.CreateOperationRequestMessageAsync();

            var(entitySet, resource) = await(new ResourceFactory(edmModel, baseUri, serviceProvider)).CreateEntryAsync(batchRequest).ConfigureAwait(false);
            return(new OeOperationMessage(entitySet, resource, batchRequest, batchRequest.ContentId));
        }
示例#2
0
        public static async ValueTask <OeOperationMessage> Create(IEdmModel edmModel, Uri baseUri, ODataBatchReader reader, IServiceProvider?serviceProvider)
        {
            ODataBatchOperationRequestMessage batchRequest = await reader.CreateOperationRequestMessageAsync();

            ODataResource entry = new ResourceFactory(edmModel, baseUri, serviceProvider).CreateEntry(batchRequest, out IEdmEntitySet entitSet);

            return(new OeOperationMessage(batchRequest, entitSet, entry));
        }
        private static async Task <HttpContext> ReadOperationInternalAsync(
            ODataBatchReader reader, HttpContext originalContext, Guid batchId, Guid?changeSetId, CancellationToken cancellationToken)
        {
            ODataBatchOperationRequestMessage batchRequest = await reader.CreateOperationRequestMessageAsync().ConfigureAwait(false);

            HttpContext context = CreateHttpContext(originalContext);
            HttpRequest request = context.Request;

            request.Method = batchRequest.Method;
            request.CopyAbsoluteUrl(batchRequest.Url);

            // Not using bufferContentStream. Unlike AspNet, AspNetCore cannot guarantee the disposal
            // of the stream in the context of execution so there is no choice but to copy the stream
            // from the batch reader.
            using (Stream stream = await batchRequest.GetStreamAsync().ConfigureAwait(false))
            {
                MemoryStream bufferedStream = new MemoryStream();
                // Passing in the default buffer size of 81920 so that we can also pass in a cancellation token
                await stream.CopyToAsync(bufferedStream, bufferSize : 81920, cancellationToken : cancellationToken).ConfigureAwait(false);

                bufferedStream.Position = 0;
                request.Body            = bufferedStream;
                if (bufferedStream.Length > 0)
                {
                    request.Headers.ContentLength = bufferedStream.Length;
                }
            }

            foreach (var header in batchRequest.Headers)
            {
                string headerName  = header.Key;
                string headerValue = header.Value;

                if (headerName.Trim().ToUpperInvariant() == "PREFER")
                {
                    // in the case of Prefer header, we don't want to overwrite,
                    // instead we merge preferences defined in the individual request with those inherited from the batch
                    request.Headers.TryGetValue(headerName, out StringValues batchReferences);
                    request.Headers[headerName] = MergeIndividualAndBatchPreferences(headerValue, batchReferences);
                }
                else
                {
                    // Copy headers from batch, overwriting any existing headers.
                    request.Headers[headerName] = headerValue;
                }
            }

            request.SetODataBatchId(batchId);
            request.SetODataContentId(batchRequest.ContentId);

            if (changeSetId != null && changeSetId.HasValue)
            {
                request.SetODataChangeSetId(changeSetId.Value);
            }

            return(context);
        }
示例#4
0
        private static async Task <HttpRequestMessage> ReadOperationInternalAsync(
            ODataBatchReader reader, Guid batchId, Guid?changeSetId, CancellationToken cancellationToken, bool bufferContentStream = true)
        {
            ODataBatchOperationRequestMessage batchRequest = await reader.CreateOperationRequestMessageAsync();

            HttpRequestMessage request = new HttpRequestMessage();

            request.Method     = new HttpMethod(batchRequest.Method);
            request.RequestUri = batchRequest.Url;

            if (bufferContentStream)
            {
                using (Stream stream = await batchRequest.GetStreamAsync())
                {
                    MemoryStream bufferedStream = new MemoryStream();
                    // Passing in the default buffer size of 81920 so that we can also pass in a cancellation token
                    await stream.CopyToAsync(bufferedStream, bufferSize : 81920, cancellationToken : cancellationToken);

                    bufferedStream.Position = 0;
                    request.Content         = new StreamContent(bufferedStream);
                }
            }
            else
            {
                request.Content = new LazyStreamContent(() => batchRequest.GetStreamAsync().Result);
            }

            foreach (var header in batchRequest.Headers)
            {
                string headerName  = header.Key;
                string headerValue = header.Value;
                if (!request.Headers.TryAddWithoutValidation(headerName, headerValue))
                {
                    request.Content.Headers.TryAddWithoutValidation(headerName, headerValue);
                }
            }

            request.SetODataBatchId(batchId);
            request.SetODataContentId(batchRequest.ContentId);

            if (changeSetId != null && changeSetId.HasValue)
            {
                request.SetODataChangeSetId(changeSetId.Value);
            }

            return(request);
        }
示例#5
0
        private static async Task <HttpContext> ReadOperationInternalAsync(
            ODataBatchReader reader, HttpContext originalContext, Guid batchId, Guid?changeSetId, CancellationToken cancellationToken, bool bufferContentStream = true)
        {
            ODataBatchOperationRequestMessage batchRequest = await reader.CreateOperationRequestMessageAsync();

            HttpContext context = CreateHttpContext(originalContext);
            HttpRequest request = context.Request;

            request.Method = batchRequest.Method;
            request.CopyAbsoluteUrl(batchRequest.Url);

            // Not using bufferContentStream. Unlike AspNet, AspNetCore cannot guarantee the disposal
            // of the stream in the context of execution so there is no choice but to copy the stream
            // from the batch reader.
            using (Stream stream = batchRequest.GetStream())
            {
                MemoryStream bufferedStream = new MemoryStream();
                // Passing in the default buffer size of 81920 so that we can also pass in a cancellation token
                await stream.CopyToAsync(bufferedStream, bufferSize : 81920, cancellationToken : cancellationToken);

                bufferedStream.Position = 0;
                request.Body            = bufferedStream;
            }

            foreach (var header in batchRequest.Headers)
            {
                // Copy headers from batch, overwriting any existing headers.
                string headerName  = header.Key;
                string headerValue = header.Value;
                request.Headers[headerName] = headerValue;
            }

            request.SetODataBatchId(batchId);
            request.SetODataContentId(batchRequest.ContentId);

            if (changeSetId != null && changeSetId.HasValue)
            {
                request.SetODataChangeSetId(changeSetId.Value);
            }

            return(context);
        }
        static async Task <HttpContext> ReadOperationInternalAsync(
            ODataBatchReader reader, HttpContext originalContext, Guid batchId, Guid?changeSetId, CancellationToken cancellationToken, bool bufferContentStream = true)
        {
            ODataBatchOperationRequestMessage batchRequest = await reader.CreateOperationRequestMessageAsync();

            HttpContext context = CreateHttpContext(originalContext);
            HttpRequest request = context.Request;

            request.Method = batchRequest.Method;
            if (batchRequest.Url.IsAbsoluteUri)
            {
                request.CopyAbsoluteUrl(batchRequest.Url);
            }
            else
            {
                var pathAndQuery = batchRequest.Url.OriginalString.Split('?');
                var path         = new PathString(pathAndQuery[0]);
                if (path.StartsWithSegments(request.PathBase, out PathString remainingPath))
                {
                    path = remainingPath;
                }
                request.Path = path;

                if (pathAndQuery.Length > 1)
                {
                    request.QueryString = new QueryString("?" + pathAndQuery[1]);
                }
            }



            // Not using bufferContentStream. Unlike AspNet, AspNetCore cannot guarantee the disposal
            // of the stream in the context of execution so there is no choice but to copy the stream
            // from the batch reader.
            using (Stream stream = await batchRequest.GetStreamAsync())
            {
                MemoryStream bufferedStream = new MemoryStream();
                // Passing in the default buffer size of 81920 so that we can also pass in a cancellation token
                await stream.CopyToAsync(bufferedStream, bufferSize : 81920, cancellationToken : cancellationToken);

                bufferedStream.Position = 0;
                request.Body            = bufferedStream;
            }

            foreach (var header in batchRequest.Headers)
            {
                string headerName  = header.Key;
                string headerValue = header.Value;

                if (headerName.Trim().ToLowerInvariant() == "prefer")
                {
                    // in the case of Prefer header, we don't want to overwrite,
                    // instead we merge preferences defined in the individual request with those inherited from the batch
                    request.Headers.TryGetValue(headerName, out StringValues batchReferences);
                    request.Headers[headerName] = MergeIndividualAndBatchPreferences(headerValue, batchReferences);
                }
                else
                {
                    // Copy headers from batch, overwriting any existing headers.
                    request.Headers[headerName] = headerValue;
                }
            }

            request.SetODataBatchId(batchId);
            request.SetODataContentId(batchRequest.ContentId);

            if (changeSetId != null && changeSetId.HasValue)
            {
                request.SetODataChangeSetId(changeSetId.Value);
            }

            return(context);
        }