Reading incoming multipart content

When using this snippet the thread goes into some infinite loop.


// Handle multipart
if (Request.Content.IsMimeMultipartContent())
{
   IEnumerable parts
     = Request
       .Content
       .ReadAsMultipartAsync()
       .Result
       .Contents;
   foreach (var part in parts)
   {
      // Do stuff with the part
   }
}

You can use the following to prevent this:

        public HttpResponseMessage Post()
        {
            // Handle multipart
            if (Request.Content.IsMimeMultipartContent())
            {
                IEnumerable parts = null;

                Task.Factory
                    .StartNew(() => parts = Request.Content.ReadAsMultipartAsync().Result.Contents,
                        CancellationToken.None,
                        TaskCreationOptions.LongRunning, // guarantees separate thread
                        TaskScheduler.Default)
                    .Wait();

                foreach (var part in parts)
                {
                    // Do stuff with the part
                }
            }
            return Request.CreateResponse(HttpStatusCode.OK, "Yeah, some text here...");
        }