silverlight, using Observables on WCF calls, casting IEvent<T> where T : AsyncCompletedEventArgs -
i have question using observables in silverlight 4 make wcf calls. consider simple webservice call below.
var adminclient = serviceproxy.withfactory<authenticationclient>(); var results= observable.fromevent<authorizeadministratorcompletedeventargs>( s => adminclient.authorizeadministratorcompleted += s, s => adminclient.authorizeadministratorcompleted -= s).observeondispatcher(); adminclient.authorizeadministratorasync(); results.subscribe(e => { //enable ui button });
i have implemented extension method, wraps subscribe method, error validation on return.
on return results.subscribe(e =>
e system.collections.generic.event<authorizeadministratorcompletedeventargs>
almost every query have different return type such as:
e system.collections.generic.event<asynccompletedeventargs>
if had wrapper looked this, how can cast every type of xxxcompletedeventargs base type asynccompletedeventargs can access e.eventargs , inspect error property
public static idisposable subscribe<tsource>(this iobservable<tsource> source, action<tsource> onnext = null, action onerror = null, action onfinal = null) { action<tsource> onnextwrapper = (s) => { var args = (system.collections.generic.ievent<asynccompletedeventargs>)s; try { if (wcfexceptionhandler.handleerror(args.eventargs)) { if (onnext != null) onnext(s); } else { if (onerror != null) onerror(); } } { if (onfinal != null) onfinal(); } }; return source.subscribe<tsource>(onnextwrapper, (e) => { throw e; }); }
the code above fail
unable cast object of type 'system.collections.generic.event
1[myproject.provider.authorizeadministratorcompletedeventargs]' type 'system.collections.generic.ievent1[system.componentmodel.asynccompletedeventargs]'
this method definition of wcfexceptionhandler.handleerror(args.eventargs))
public static bool handleerror(asynccompletedeventargs e)
i'd change extension method acts handle the events non blocking operator (much same majority of rx extension method operators). like:
public static iobservable<ievent<teventargs>> gethandledevents<teventargs>(this iobservable<ievent<teventargs>> source) teventargs : asynccompletedeventargs { return observable.createwithdisposable<ievent<teventargs>>(observer => { return source.subscribe(evt => { try { if (wcfexceptionhandler.handleerror(evt.eventargs)) { observer.onnext(evt); } else { observer.onerror(new exception("some exception")); } } { observer.onerror(new exception("some other exception")); } }, observer.onerror, observer.oncompleted); }); }
then call through:
results.gethandledevents() .finally(() => dosomethingfinally()) .subscribe(e => { //enable ui button }, ex => handleexception(ex), () => handlecomplete());
i think should solve issues events funnel through original type , ensures handleerror
gets event args correct type.
Comments
Post a Comment