Skip to main content

Posts

Showing posts with the label TPL

Exception handling for an async method

I've been unit testing a method which has a return type of Task<T> and I need to handle cases where  exceptions will be thrown. Should I use the Task approach or convert to an Rx observable stream? Task try-catch approach: or Rx declarative style: I know which one I prefer :)

Using GetRequestStreamAsync and GetResponseAsync in .Net 4.0 Portable Class Library

I've been building a small Portable Class Library  and it makes use of the WebRequest class. I'm targeting .Net 4.0 (and above), Silverlight 5 and Windows App Store. I would target the phone as well but it doesn't support the TPL  at the moment - WP8 is just around the corner: I wanted to use async method GetRequestStreamAsync  & GetResponseAsync but these aren't supported in .Net 4.0 and when creating a Portable Class Library you only get the commonly supported methods across the configured platforms - so no support then or may be not... Why not just create a couple of extension methods? 1: public static class HttpWebRequestExtensions 2: { 3: public static Task<Stream> GetRequestStreamAsync( this HttpWebRequest request) 4: { 5: var tcs = new TaskCompletionSource<Stream>(); 6:   7: try 8: { 9: request.BeginGetRequestStream(iar => ...