system.reactive - Managing async service calls using Silverlight and Reactive Extensions -
so i'm reading on rx , having difficult time grokking it. have silverlight app needs make 6 calls specific service asynchronously. in old days, we'd handle making calls , querying userstate/token match response request since they're not guaranteed return in order called them. however, suspect rx handles in far more elegant manner. can't work. here's have far...
mycollection.add(new myobject(1)); mycollection.add(new myobject(2)); mycollection.add(new myobject(3)); mycollection.add(new myobject(4)); mycollection.add(new myobject(5)); mycollection.add(new myobject(6)); foreach (var myitem in mycollection) { var myobservable = observable.fromeventpattern<myservicemethodcompletedeventargs> ( f => myserviceclient.myservicemethodcompleted += f, f => myserviceclient.myservicemethodcompleted -= f ).take(1).observeon(synchronizationcontext.current); myobservable.subscribe ( s => { if (s.eventargs.error == null) { myitem.myproperty = s.eventargs.result; } } ); myserviceclient.myservicemethodasync(myitem); }
i hope can see i'm trying achieve here...
what end of myobject's being set result of first call returns.
i'm sure it's silly haven't been able figure out yet.
thanks :)
consider trying observable.fromasyncpattern instead of observable.fromeventpattern. there trick using fromasyncpattern in silverlight (and phone) because begininvoke/endinvoke pair not exposed directly service proxy. however, if use interface service proxy rather service proxy itself, can access begin/end pattern:
imyservice svc = new myserviceclient(); var svcobservable = observable.fromasyncpattern<t, myserviceresultargs> (svc.beginmyservicemethod, svc.endmyservicemethod);
now, can switch using foreach (an anti-pattern linq) making mycollection observable , selectmany between mycollection , service request follows:
var requestresult = myitem in mycollection.toobservable() result in svcobservable(myitem) select new {myitem, result}; requestresult.subscribe(result => result.myitem.myproperty = result.result);
one additional word of caution: if use fromasyncpattern in silverlight way, result come on background thread. need take care teo delegate dispatcher.
if want see in action, check out last 20 minutes or of mix presentation @ http://channel9.msdn.com/events/mix/mix11/ext08.
Comments
Post a Comment