/// <summary> /// Initializes a new instance of the <see cref="MultipartAsyncContext"/> class. /// </summary> /// <param name="contentStream">The content stream.</param> /// <param name="taskCompletionSource">The task completion source.</param> /// <param name="mimeParser">The MIME parser.</param> /// <param name="data">The buffer that we read data from.</param> public MultipartAsyncContext(Stream contentStream, TaskCompletionSource<IEnumerable<HttpContent>> taskCompletionSource, MimeMultipartBodyPartParser mimeParser, byte[] data) { Contract.Assert(contentStream != null, "contentStream cannot be null"); Contract.Assert(taskCompletionSource != null, "task cannot be null"); Contract.Assert(mimeParser != null, "mimeParser cannot be null"); Contract.Assert(data != null, "data cannot be null"); ContentStream = contentStream; Result = new List<HttpContent>(); TaskCompletionSource = taskCompletionSource; MimeParser = mimeParser; Data = data; }
public static Task<IEnumerable<HttpContent>> ReadAsMultipartAsync(this HttpContent content, IMultipartStreamProvider streamProvider, int bufferSize) { if (content == null) { throw new ArgumentNullException("content"); } if (streamProvider == null) { throw new ArgumentNullException("streamProvider"); } if (bufferSize < MinBufferSize) { throw new ArgumentOutOfRangeException("bufferSize", bufferSize, RS.Format(Properties.Resources.ArgumentMustBeGreaterThanOrEqualTo, MinBufferSize)); } return content.ReadAsStreamAsync().Then(stream => { TaskCompletionSource<IEnumerable<HttpContent>> taskCompletionSource = new TaskCompletionSource<IEnumerable<HttpContent>>(); MimeMultipartBodyPartParser parser = new MimeMultipartBodyPartParser(content, streamProvider); byte[] data = new byte[bufferSize]; MultipartAsyncContext context = new MultipartAsyncContext(stream, taskCompletionSource, parser, data); // Start async read/write loop MultipartReadAsync(context); // Return task and complete later return taskCompletionSource.Task; }); }