Skip to main content

Posts

Showing posts with the label Developement

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 => ...

Using CompositeDisposable in base classes

To help make an object eligible for collection by the GC (garbage collector) one would implement the IDisposable interface. Executing the dispose methods on muliple implmentations at the correct same time is often done using a custom DisposeWith extension method: 1: public static class DisposableExtensions 2: { 3: public static T DisposeWith<T>( this T disposable, CompositeDisposable disposables) where T : IDisposable 4: { 5: disposables.Add(disposable); 6:   7: return disposable; 8: } 9: } Typically used as follows - explicitly creating a CompositeDisposable instance and then disposing the child view model with the instance: Firstly can I get away without having to explicitly create an instance? Also can I get to a point where I can just do something like this: Inheritance seems the obvious choice but as you might already know, all the disposable types in the System.Rea...

How do I flatten an enumerable IObservable to IObservable?

The title of this post should be  'How do I flatten IObservable<IEnumerable <T> > to IObservable<T>?' But blogger says 'no'... This seems like a simple & legitimate Rx question and if you're one of the initiated it's very easy simple. It came up the other day because a back-end service we're talking to returned an array of stuff and we wanted to flatten it into a stream of single values we could observe. So what do I replace CONVERT_SOMEHOW with? voilĂ ... 1: IObservable< int []> numbers = Observable.Return( new [] {1, 2, 3, 4, 5}); 2:   3: IObservable< int > number = numbers.SelectMany(n => n); In a simple app: produces the following output: