Thursday, August 2, 2012

Errors encountered on Rx (Reactive Extensions) HOL (Hands on Lab)

Problems doing HOL
Error doing HOL
Hands on labs errors

Reactive extensions (Rx) in .Net intends to solve the nightmares of asynchronous programming. The concept is really new to me and I tried doing the Rx Team's HOL (Hands on Lab) for Rx in C#. Since the HOL was last updated a number of changes have been made. These are the troubles I encountered while doing the Rx HOL.

 1.
source.Run( x => Console.WriteLine("OnNext: {0}", x), ex => Console.WriteLine("OnError: {0}", ex.Message), () => Console.WriteLine("OnCompleted") );
Solution: Use ForEach or Do.
See: http://social.msdn.microsoft.com/Forums/en-US/rx/thread/398452c2-c97d-4d96-b784-2a2e8a026d12



2. Cannot create a breakpoint inside lambda expression, lambda statement:

Solution: The answer is actually on the HOL pdf. But if you are stupid like me and thought you could set a breakpoint the usual way then how wrong you are. It took me a few minutes to find out that a breakpoint cannot be set inside a lambda expression by clicking on the grey bar or by Pressing F9. The way to do it is to click inside a statement then press F9.

e.g. Click on the OnNext word inside WriteLine() then press F9.


3.  
var moves = Observable.FromEvent(frm, "MouseMove");
Solution: Use FromEventPattern instead. 
var moves = Observable.FromEventPattern(frm, "MouseMove");

4.
using (new CompositeDisposable(movesSubscription, inputSubscription))
Solution: Fully qualify it.
 using (new System.Reactive.Disposables.CompositeDisposable(movesSubscription, inputSubscription))

5.
var moves = Observable.FromEvent(frm, "MouseMove"); 
var input = Observable.FromEvent(txt, "TextChanged");
Solution: Same as 3. Use  FromEventPattern
var moves = Observable.FromEventPattern(frm, "MouseMove"); 
var input = Observable.FromEventPattern(txt, "TextChanged");

6. ObserveOn(lbl) error HOL.
using (input.ObserveOn(lbl).Subscribe(inp => lbl.Text = inp))
Solution: Make sure you add System.Reactive.Windows.Forms as a reference

7. DictServiceSoapClient not found
var svc = new DictServiceSoapClient("DictServiceSoap");
Solution: Add a add a "using"  to the header with the namespace of the service reference you created.
e.g.  using RxLab.DictionarySuggestService;


Tags: C# RX, c# reactive extensions