Esempio n. 1
0
        /// <summary>
        /// Executes the ChangeSet.
        /// </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="cancellationToken">The token to monitor for cancellation requests.</param>
        /// <returns>The response for the ChangeSet.</returns>
        public virtual async Task <ODataBatchResponseItem> ExecuteChangeSetAsync(ODataBatchReader batchReader, Guid batchId, HttpRequestMessage originalRequest, CancellationToken cancellationToken)
        {
            if (batchReader == null)
            {
                throw Error.ArgumentNull("batchReader");
            }
            if (originalRequest == null)
            {
                throw Error.ArgumentNull("originalRequest");
            }

            Guid changeSetId = Guid.NewGuid();
            List <HttpResponseMessage>  changeSetResponse          = new List <HttpResponseMessage>();
            Dictionary <string, string> contentIdToLocationMapping = new Dictionary <string, string>();

            try
            {
                while (batchReader.Read() && batchReader.State != ODataBatchReaderState.ChangesetEnd)
                {
                    if (batchReader.State == ODataBatchReaderState.Operation)
                    {
                        HttpRequestMessage changeSetOperationRequest = await batchReader.ReadChangeSetOperationRequestAsync(batchId, changeSetId, bufferContentStream : false);

                        changeSetOperationRequest.CopyBatchRequestProperties(originalRequest);
                        changeSetOperationRequest.DeleteRequestContainer(false);
                        try
                        {
                            HttpResponseMessage response = await ODataBatchRequestItem.SendMessageAsync(Invoker, changeSetOperationRequest, cancellationToken, contentIdToLocationMapping);

                            if (response.IsSuccessStatusCode)
                            {
                                changeSetResponse.Add(response);
                            }
                            else
                            {
                                ChangeSetRequestItem.DisposeResponses(changeSetResponse);
                                changeSetResponse.Clear();
                                changeSetResponse.Add(response);
                                return(new ChangeSetResponseItem(changeSetResponse));
                            }
                        }
                        finally
                        {
                            originalRequest.RegisterForDispose(changeSetOperationRequest.GetResourcesForDisposal());
                            originalRequest.RegisterForDispose(changeSetOperationRequest);
                        }
                    }
                }
            }
            catch
            {
                ChangeSetRequestItem.DisposeResponses(changeSetResponse);
                throw;
            }

            return(new ChangeSetResponseItem(changeSetResponse));
        }
Esempio n. 2
0
        /// <summary>
        /// Executes the ChangeSet.
        /// </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 ChangeSet.</returns>
        public virtual async Task <ODataBatchResponseItem> ExecuteChangeSetAsync(ODataBatchReader batchReader, Guid batchId, HttpRequest originalRequest, RequestDelegate handler)
        {
            if (batchReader == null)
            {
                throw Error.ArgumentNull("batchReader");
            }
            if (originalRequest == null)
            {
                throw Error.ArgumentNull("originalRequest");
            }
            if (handler == null)
            {
                throw Error.ArgumentNull("handler");
            }

            Guid changeSetId = Guid.NewGuid();
            List <HttpContext>          changeSetResponse          = new List <HttpContext>();
            Dictionary <string, string> contentIdToLocationMapping = new Dictionary <string, string>();

            while (await batchReader.ReadAsync() && batchReader.State != ODataBatchReaderState.ChangesetEnd)
            {
                if (batchReader.State == ODataBatchReaderState.Operation)
                {
                    CancellationToken cancellationToken         = originalRequest.HttpContext.RequestAborted;
                    HttpContext       changeSetOperationContext = await batchReader.ReadChangeSetOperationRequestAsync(originalRequest.HttpContext, batchId, changeSetId, false, cancellationToken);

                    changeSetOperationContext.Request.CopyBatchRequestProperties(originalRequest);
                    changeSetOperationContext.Request.DeleteRequestContainer(false);

                    await ODataBatchRequestItem.SendRequestAsync(handler, changeSetOperationContext, contentIdToLocationMapping);

                    if (changeSetOperationContext.Response.IsSuccessStatusCode())
                    {
                        changeSetResponse.Add(changeSetOperationContext);
                    }
                    else
                    {
                        changeSetResponse.Clear();
                        changeSetResponse.Add(changeSetOperationContext);
                        return(new ChangeSetResponseItem(changeSetResponse));
                    }
                }
            }

            return(new ChangeSetResponseItem(changeSetResponse));
        }