/// <summary>
        /// Executes the operation.
        /// </summary>
        /// <param name="batchReader">The batch reader.</param>
        /// <param name="batchId">The batch id.</param>
        /// <param name="originalRequest">The original request containing all the batch requests.</param>
        /// <param name="handler">The handler for processing a message.</param>
        /// <returns>The response for the operation.</returns>
        public virtual async Task <ODataBatchResponseItem> ExecuteOperationAsync(ODataBatchReader batchReader, Guid batchId, HttpRequest originalRequest, RequestDelegate handler)
        {
            if (batchReader == null)
            {
                throw new ArgumentNullException(nameof(batchReader));
            }

            if (originalRequest == null)
            {
                throw new ArgumentNullException(nameof(originalRequest));
            }

            if (handler == null)
            {
                throw new ArgumentNullException(nameof(handler));
            }

            CancellationToken cancellationToken = originalRequest.HttpContext.RequestAborted;

            cancellationToken.ThrowIfCancellationRequested();
            HttpContext operationContext = await batchReader.ReadOperationRequestAsync(originalRequest.HttpContext, batchId, cancellationToken).ConfigureAwait(false);

            operationContext.Request.ClearRouteServices();
            OperationRequestItem operation = new OperationRequestItem(operationContext);

            ODataBatchResponseItem responseItem = await operation.SendRequestAsync(handler).ConfigureAwait(false);

            return(responseItem);
        }
Exemple #2
0
        /// <summary>
        /// Converts the incoming OData batch request into a collection of request messages.
        /// </summary>
        /// <param name="context">The context containing the batch request messages.</param>
        /// <returns>A collection of <see cref="ODataBatchRequestItem"/>.</returns>
        public virtual async Task <IList <ODataBatchRequestItem> > ParseBatchRequestsAsync(HttpContext context)
        {
            if (context == null)
            {
                throw Error.ArgumentNull(nameof(context));
            }

            HttpRequest      request          = context.Request;
            IServiceProvider requestContainer = request.CreateRouteServices(PrefixName);

            requestContainer.GetRequiredService <ODataMessageReaderSettings>().BaseUri = GetBaseUri(request);

            using (ODataMessageReader reader = request.GetODataMessageReader(requestContainer))
            {
                CancellationToken            cancellationToken = context.RequestAborted;
                List <ODataBatchRequestItem> requests          = new List <ODataBatchRequestItem>();
                ODataBatchReader             batchReader       = await reader.CreateODataBatchReaderAsync().ConfigureAwait(false);

                Guid batchId = Guid.NewGuid();
                Dictionary <string, string> contentToLocationMapping = new Dictionary <string, string>();

                while (await batchReader.ReadAsync().ConfigureAwait(false))
                {
                    if (batchReader.State == ODataBatchReaderState.ChangesetStart)
                    {
                        IList <HttpContext> changeSetContexts = await batchReader.ReadChangeSetRequestAsync(context, batchId, cancellationToken).ConfigureAwait(false);

                        foreach (HttpContext changeSetContext in changeSetContexts)
                        {
                            changeSetContext.Request.ClearRouteServices();
                        }

                        ChangeSetRequestItem requestItem = new ChangeSetRequestItem(changeSetContexts);
                        requestItem.ContentIdToLocationMapping = contentToLocationMapping;
                        requests.Add(requestItem);
                    }
                    else if (batchReader.State == ODataBatchReaderState.Operation)
                    {
                        HttpContext operationContext = await batchReader.ReadOperationRequestAsync(context, batchId, cancellationToken).ConfigureAwait(false);

                        operationContext.Request.ClearRouteServices();
                        OperationRequestItem requestItem = new OperationRequestItem(operationContext);
                        requestItem.ContentIdToLocationMapping = contentToLocationMapping;
                        requests.Add(requestItem);
                    }
                }

                return(requests);
            }
        }