PushStreamContent and async lambdas

Topics: ASP.NET Web API
Feb 27, 2013 at 1:32 PM
Edited Feb 27, 2013 at 1:33 PM
Hi, folks -

PushStreamContent only takes Actions as constructor parameters, so async lambdas are interpreted as async void methods. This causes problems with exception handling and can also cause race conditions (http://stackoverflow.com/questions/15060214/web-api-httpclient-an-asynchronous-module-or-handler-completed-while-an-async).

Would you like me to contribute a fix? It would be rather simple and would not affect any other types. The constructor would be overloaded with Func types, and the "completion" logic changed so that the stream must be disposed and the method run to completion. This is slightly different than how PushStreamContent handles async lambdas today, but after this fix it would treat async lambdas the same as sync lambdas. I'll also supply unit tests for regression.

-Steve
Feb 28, 2013 at 5:05 AM
Having an async lambda overload would be great! FWIW, the example on SO doesn't actually require the PushStreamContent -- it can just pass the HttpContents around. I added a sample "ASP.NET Web API sample showing how to relay requests and responses asynchronously and without buffering" (http://aspnet.codeplex.com/SourceControl/changeset/view/7ce67a547fd0#Samples/WebApi/RelaySample/ReadMe.txt) to illustrate this.

Henrik