/// <summary> /// Asynchronously detects the payload kinds supported by this format for the specified message payload. /// </summary> /// <param name="requestMessage">The request message with the payload stream.</param> /// <param name="detectionInfo">Additional information available for the payload kind detection.</param> /// <returns>A task that when completed returns the set of <see cref="ODataPayloadKind"/>s /// that are supported with the specified payload.</returns> internal override Task <IEnumerable <ODataPayloadKind> > DetectPayloadKindAsync( IODataRequestMessageAsync requestMessage, ODataPayloadKindDetectionInfo detectionInfo) { DebugUtils.CheckNoExternalCallers(); ExceptionUtils.CheckArgumentNotNull(requestMessage, "requestMessage"); ExceptionUtils.CheckArgumentNotNull(detectionInfo, "detectionInfo"); // Metadata is not supported in requests! return(TaskUtils.GetCompletedTask(Enumerable.Empty <ODataPayloadKind>())); }
protected internal Task <Stream> GetStreamAsync(Func <Task <Stream> > streamFuncAsync, bool isRequest) { Func <Task <Stream>, Stream> operation = null; if (!this.writing) { Stream stream = this.TryGetBufferingReadStream(); if (stream != null) { return(TaskUtils.GetCompletedTask <Stream>(stream)); } } Task <Stream> task = streamFuncAsync(); ValidateMessageStreamTask(task, isRequest); task = task.FollowOnSuccessWith <Stream, Stream>(delegate(Task <Stream> streamTask) { Stream result = streamTask.Result; ValidateMessageStream(result, isRequest); bool flag = !this.writing && (this.maxMessageSize > 0L); if (this.disableMessageStreamDisposal && flag) { return(MessageStreamWrapper.CreateNonDisposingStreamWithMaxSize(result, this.maxMessageSize)); } if (this.disableMessageStreamDisposal) { return(MessageStreamWrapper.CreateNonDisposingStream(result)); } if (flag) { result = MessageStreamWrapper.CreateStreamWithMaxSize(result, this.maxMessageSize); } return(result); }); if (this.writing) { return(task); } task = task.FollowOnSuccessWithTask <Stream, BufferedReadStream>(streamTask => BufferedReadStream.BufferStreamAsync(streamTask.Result)).FollowOnSuccessWith <BufferedReadStream, Stream>(streamTask => streamTask.Result); if (this.useBufferingReadStream != true) { return(task); } if (operation == null) { operation = delegate(Task <Stream> streamTask) { Stream result = streamTask.Result; this.bufferingReadStream = new Microsoft.Data.OData.BufferingReadStream(result); return(this.bufferingReadStream); }; } return(task.FollowOnSuccessWith <Stream, Stream>(operation)); }
public Task <IEnumerable <ODataPayloadKindDetectionResult> > DetectPayloadKindAsync() { IEnumerable <ODataPayloadKindDetectionResult> enumerable; MediaType type; if (this.settings.ReaderBehavior.ApiBehaviorKind == ODataBehaviorKind.WcfDataServicesServer) { throw new ODataException(Microsoft.Data.OData.Strings.ODataMessageReader_PayloadKindDetectionInServerMode); } if (this.TryGetSinglePayloadKindResultFromContentType(out enumerable, out type)) { return(TaskUtils.GetCompletedTask <IEnumerable <ODataPayloadKindDetectionResult> >(enumerable)); } List <ODataPayloadKindDetectionResult> detectedPayloadKinds = new List <ODataPayloadKindDetectionResult>(); return(Task.Factory.Iterate(this.GetPayloadKindDetectionTasks(type, enumerable, detectedPayloadKinds)).FollowAlwaysWith(delegate(Task t) { this.message.UseBufferingReadStream = false; this.message.BufferingReadStream.StopBuffering(); }).FollowOnSuccessWith <IEnumerable <ODataPayloadKindDetectionResult> >(delegate(Task t) { detectedPayloadKinds.Sort(new Comparison <ODataPayloadKindDetectionResult>(this.ComparePayloadKindDetectionResult)); return detectedPayloadKinds; })); }
/// <summary> /// Asynchronously get the stream backing this message. /// </summary> /// <param name="streamFuncAsync">A function that returns a task for the stream backing the message.</param> /// <param name="isRequest">true if the message is a request message; false for a response message.</param> /// <returns>A task that when completed returns the stream backing the message.</returns> protected internal Task <Stream> GetStreamAsync(Func <Task <Stream> > streamFuncAsync, bool isRequest) { // Check whether we have an existing buffering read stream when reading if (!this.writing) { Stream existingBufferingReadStream = this.TryGetBufferingReadStream(); Debug.Assert(!this.writing || existingBufferingReadStream == null, "The buffering read stream should only be used when reading."); if (existingBufferingReadStream != null) { Debug.Assert(this.useBufferingReadStream.HasValue, "UseBufferingReadStream must have been set."); return(TaskUtils.GetCompletedTask(existingBufferingReadStream)); } } Task <Stream> task = streamFuncAsync(); ValidateMessageStreamTask(task, isRequest); // Wrap it in a non-disposing stream if requested task = task.FollowOnSuccessWith( streamTask => { Stream messageStream = streamTask.Result; ValidateMessageStream(messageStream, isRequest); // When reading, wrap the stream in a byte counting stream if a max message size was specified. // When requested, wrap the stream in a non-disposing stream. bool needByteCountingStream = !this.writing && this.maxMessageSize > 0; if (this.disableMessageStreamDisposal && needByteCountingStream) { messageStream = MessageStreamWrapper.CreateNonDisposingStreamWithMaxSize(messageStream, this.maxMessageSize); } else if (this.disableMessageStreamDisposal) { messageStream = MessageStreamWrapper.CreateNonDisposingStream(messageStream); } else if (needByteCountingStream) { messageStream = MessageStreamWrapper.CreateStreamWithMaxSize(messageStream, this.maxMessageSize); } return(messageStream); }); // When we are reading, also buffer the input stream if (!this.writing) { task = task .FollowOnSuccessWithTask( streamTask => { return(BufferedReadStream.BufferStreamAsync(streamTask.Result)); }) .FollowOnSuccessWith( streamTask => { BufferedReadStream bufferedReadStream = streamTask.Result; return((Stream)bufferedReadStream); }); // If requested also create a buffering stream for payload kind detection if (this.useBufferingReadStream == true) { task = task.FollowOnSuccessWith( streamTask => { Stream messageStream = streamTask.Result; this.bufferingReadStream = new BufferingReadStream(messageStream); messageStream = this.bufferingReadStream; return(messageStream); }); } } return(task); }
internal override Task <IEnumerable <ODataPayloadKind> > DetectPayloadKindAsync(IODataRequestMessageAsync requestMessage, ODataPayloadKindDetectionInfo detectionInfo) { ExceptionUtils.CheckArgumentNotNull <IODataRequestMessageAsync>(requestMessage, "requestMessage"); ExceptionUtils.CheckArgumentNotNull <ODataPayloadKindDetectionInfo>(detectionInfo, "detectionInfo"); return(TaskUtils.GetCompletedTask <IEnumerable <ODataPayloadKind> >(Enumerable.Empty <ODataPayloadKind>())); }