request.Content.ContentReadStream equivalent on Preview 6

Topics: Web Api
Dec 6, 2011 at 11:33 AM

I was using a method like below on the previous versions of WCF Web API:

 

                // grab the posted stream
                Stream stream = request.Content.ContentReadStream;

                // write it to   
                using (FileStream fileStream = File.Create(fullFileName, (int)stream.Length)) {

                    byte[] bytesInStream = new byte[stream.Length];
                    stream.Read(bytesInStream, 0, (int)bytesInStream.Length);
                    fileStream.Write(bytesInStream, 0, bytesInStream.Length);
                }

But on the preview 6, ContentReadStream property is gone. I believe that it now should look like this one:

                // grab the posted stream
                System.Threading.Tasks.Task<Stream> stream = request.Content.ReadAsStreamAsync();

But I couldn't figure out what the rest of the code should be like inside the using statement.

 

Dec 6, 2011 at 12:48 PM

Found the solution:

                Task task = request.Content.ReadAsStreamAsync().ContinueWith(t => {

                    var stream = t.Result;

                    using (FileStream fileStream = File.Create(fullFileName, (int)stream.Length)) {

                        byte[] bytesInStream = new byte[stream.Length];
                        stream.Read(bytesInStream, 0, (int)bytesInStream.Length);
                        fileStream.Write(bytesInStream, 0, bytesInStream.Length);
                    }

                });

                task.Wait();

The complete discussion on this topic is here as well:

http://stackoverflow.com/questions/8399994/working-with-system-threading-tasks-taskstream-instead-of-stream