/// <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); }
/// <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="cancellationToken">The token to monitor for cancellation requests.</param> /// <returns>The response for the operation.</returns> public virtual async Task <ODataBatchResponseItem> ExecuteOperationAsync(ODataBatchReader batchReader, Guid batchId, HttpRequestMessage originalRequest, CancellationToken cancellationToken) { if (batchReader == null) { throw Error.ArgumentNull("batchReader"); } if (originalRequest == null) { throw Error.ArgumentNull("originalRequest"); } cancellationToken.ThrowIfCancellationRequested(); HttpRequestMessage operationRequest = await batchReader.ReadOperationRequestAsync(batchId, bufferContentStream : false); operationRequest.CopyBatchRequestProperties(originalRequest); OperationRequestItem operation = new OperationRequestItem(operationRequest); try { ODataBatchResponseItem response = await operation.SendRequestAsync(Invoker, cancellationToken); return(response); } finally { originalRequest.RegisterForDispose(operation.GetResourcesForDisposal()); originalRequest.RegisterForDispose(operation); } }
/// <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 Error.ArgumentNull("batchReader"); } if (originalRequest == null) { throw Error.ArgumentNull("originalRequest"); } if (handler == null) { throw Error.ArgumentNull("handler"); } CancellationToken cancellationToken = originalRequest.HttpContext.RequestAborted; cancellationToken.ThrowIfCancellationRequested(); HttpContext operationContext = await batchReader.ReadOperationRequestAsync(originalRequest.HttpContext, batchId, false, cancellationToken); operationContext.Request.CopyBatchRequestProperties(originalRequest); operationContext.Request.DeleteRequestContainer(false); OperationRequestItem operation = new OperationRequestItem(operationContext); ODataBatchResponseItem responseItem = await operation.SendRequestAsync(handler); return(responseItem); }
/// <summary> /// Asynchronously parses the batch requests. /// </summary> /// <param name="request">The HTTP request that contains the batch requests.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>The task object that represents this asynchronous operation.</returns> public override async Task <IList <ODataBatchRequestItem> > ParseBatchRequestsAsync( HttpRequestMessage request, CancellationToken cancellationToken) { if (this.ApiFactory == null) { throw new InvalidOperationException(Resources.BatchHandlerRequiresApiContextFactory); } Ensure.NotNull(request, "request"); ODataMessageReaderSettings readerSettings = new ODataMessageReaderSettings { DisableMessageStreamDisposal = true, MessageQuotas = MessageQuotas, BaseUri = GetBaseUri(request) }; ODataMessageReader reader = await request.Content.GetODataMessageReaderAsync(readerSettings, cancellationToken); request.RegisterForDispose(reader); List <ODataBatchRequestItem> requests = new List <ODataBatchRequestItem>(); ODataBatchReader batchReader = reader.CreateODataBatchReader(); Guid batchId = Guid.NewGuid(); while (batchReader.Read()) { if (batchReader.State == ODataBatchReaderState.ChangesetStart) { IList <HttpRequestMessage> changeSetRequests = await batchReader.ReadChangeSetRequestAsync(batchId, cancellationToken); foreach (HttpRequestMessage changeSetRequest in changeSetRequests) { changeSetRequest.CopyBatchRequestProperties(request); } requests.Add(this.CreateChangeSetRequestItem(changeSetRequests)); } else if (batchReader.State == ODataBatchReaderState.Operation) { HttpRequestMessage operationRequest = await batchReader.ReadOperationRequestAsync( batchId, bufferContentStream : true, cancellationToken : cancellationToken); operationRequest.CopyBatchRequestProperties(request); requests.Add(new OperationRequestItem(operationRequest)); } } return(requests); }
/// <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); } }
/// <summary> /// Asynchronously parses the batch requests. /// </summary> /// <param name="request">The HTTP request that contains the batch requests.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>The task object that represents this asynchronous operation.</returns> public override async Task <IList <ODataBatchRequestItem> > ParseBatchRequestsAsync( HttpRequestMessage request, CancellationToken cancellationToken) { Ensure.NotNull(request, "request"); IServiceProvider requestContainer = request.CreateRequestContainer(ODataRouteName); requestContainer.GetRequiredService <ODataMessageReaderSettings>().BaseUri = GetBaseUri(request); ODataMessageReader reader = await request.Content.GetODataMessageReaderAsync(requestContainer, cancellationToken); request.RegisterForDispose(reader); List <ODataBatchRequestItem> requests = new List <ODataBatchRequestItem>(); ODataBatchReader batchReader = reader.CreateODataBatchReader(); Guid batchId = Guid.NewGuid(); while (batchReader.Read()) { if (batchReader.State == ODataBatchReaderState.ChangesetStart) { IList <HttpRequestMessage> changeSetRequests = await batchReader.ReadChangeSetRequestAsync(batchId, cancellationToken); foreach (HttpRequestMessage changeSetRequest in changeSetRequests) { changeSetRequest.CopyBatchRequestProperties(request); changeSetRequest.DeleteRequestContainer(false); } requests.Add(this.CreateRestierBatchChangeSetRequestItem(changeSetRequests)); } else if (batchReader.State == ODataBatchReaderState.Operation) { HttpRequestMessage operationRequest = await batchReader.ReadOperationRequestAsync( batchId, true, cancellationToken); operationRequest.CopyBatchRequestProperties(request); operationRequest.DeleteRequestContainer(false); requests.Add(new OperationRequestItem(operationRequest)); } } return(requests); }
public virtual async Task <IList <ODataBatchRequestItem> > ParseBatchRequestsAsync(HttpRequestMessage request, CancellationToken cancellationToken) { if (request == null) { throw Error.ArgumentNull("request"); } ODataMessageReaderSettings oDataReaderSettings = new ODataMessageReaderSettings { DisableMessageStreamDisposal = true, MessageQuotas = MessageQuotas, BaseUri = GetBaseUri(request) }; cancellationToken.ThrowIfCancellationRequested(); ODataMessageReader reader = await request.Content.GetODataMessageReaderAsync(oDataReaderSettings); request.RegisterForDispose(reader); List <ODataBatchRequestItem> requests = new List <ODataBatchRequestItem>(); ODataBatchReader batchReader = reader.CreateODataBatchReader(); Guid batchId = Guid.NewGuid(); while (batchReader.Read()) { if (batchReader.State == ODataBatchReaderState.ChangesetStart) { IList <HttpRequestMessage> changeSetRequests = await batchReader.ReadChangeSetRequestAsync(batchId, cancellationToken); foreach (HttpRequestMessage changeSetRequest in changeSetRequests) { changeSetRequest.CopyBatchRequestProperties(request); } requests.Add(new ChangeSetRequestItem(changeSetRequests)); } else if (batchReader.State == ODataBatchReaderState.Operation) { HttpRequestMessage operationRequest = await batchReader.ReadOperationRequestAsync(batchId, bufferContentStream : true, cancellationToken : cancellationToken); operationRequest.CopyBatchRequestProperties(request); requests.Add(new OperationRequestItem(operationRequest)); } } return(requests); }
/// <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 new ArgumentNullException(nameof(context)); } HttpRequest request = context.Request; IServiceProvider requestContainer = request.CreateSubServiceProvider(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(); 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.CopyBatchRequestProperties(request); changeSetContext.Request.DeleteSubRequestProvider(false); } requests.Add(new ChangeSetRequestItem(changeSetContexts)); } else if (batchReader.State == ODataBatchReaderState.Operation) { HttpContext operationContext = await batchReader.ReadOperationRequestAsync(context, batchId, cancellationToken).ConfigureAwait(false); operationContext.Request.CopyBatchRequestProperties(request); operationContext.Request.DeleteSubRequestProvider(false); requests.Add(new OperationRequestItem(operationContext)); } } return(requests); } }
public virtual async Task <IList <ODataBatchRequestItem> > ParseBatchRequestsAsync(HttpRequestMessage request) { if (request == null) { throw Error.ArgumentNull("request"); } ODataMessageReaderSettings oDataReaderSettings = new ODataMessageReaderSettings { DisableMessageStreamDisposal = true, MessageQuotas = MessageQuotas, BaseUri = GetBaseUri(request) }; ODataMessageReader reader = await request.Content.GetODataMessageReaderAsync(oDataReaderSettings); request.RegisterForDispose(reader); List <ODataBatchRequestItem> requests = new List <ODataBatchRequestItem>(); ODataBatchReader batchReader = reader.CreateODataBatchReader(); Guid batchId = Guid.NewGuid(); while (batchReader.Read()) { if (batchReader.State == ODataBatchReaderState.ChangesetStart) { requests.Add(new ChangeSetRequestItem(await batchReader.ReadChangeSetRequestAsync(batchId))); } else if (batchReader.State == ODataBatchReaderState.Operation) { requests.Add(new OperationRequestItem(await batchReader.ReadOperationRequestAsync(batchId, bufferContentStream: true))); } } return(requests); }
/// <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="cancellationToken">The token to monitor for cancellation requests.</param> /// <returns>The response for the operation.</returns> public virtual async Task<ODataBatchResponseItem> ExecuteOperationAsync(ODataBatchReader batchReader, Guid batchId, HttpRequestMessage originalRequest, CancellationToken cancellationToken) { if (batchReader == null) { throw Error.ArgumentNull("batchReader"); } if (originalRequest == null) { throw Error.ArgumentNull("originalRequest"); } cancellationToken.ThrowIfCancellationRequested(); HttpRequestMessage operationRequest = await batchReader.ReadOperationRequestAsync(batchId, bufferContentStream: false); operationRequest.CopyBatchRequestProperties(originalRequest); OperationRequestItem operation = new OperationRequestItem(operationRequest); try { ODataBatchResponseItem response = await operation.SendRequestAsync(Invoker, cancellationToken); return response; } finally { originalRequest.RegisterForDispose(operation.GetResourcesForDisposal()); originalRequest.RegisterForDispose(operation); } }
/// <summary> /// Reads an Operation request. /// </summary> /// <param name="reader">The <see cref="ODataBatchReader"/>.</param> /// <param name="batchId">The Batch ID.</param> /// <param name="bufferContentStream">if set to <c>true</c> then the request content stream will be buffered.</param> /// <returns>A <see cref="HttpRequestMessage"/> representing the operation.</returns> public static Task <HttpRequestMessage> ReadOperationRequestAsync(this ODataBatchReader reader, Guid batchId, bool bufferContentStream) { return(reader.ReadOperationRequestAsync(batchId, bufferContentStream, CancellationToken.None)); }