c# - How do I make an asynchronous HTTP request with RxNET? -
currently i'm using await inside observable.create(). executerequestasync wrapper class call httpclient.getasync method (string)
public iobservable<ilist<exampleresponsemodel>> listexamplesrx() { return observable.create<ilist<exampleresponsemodel>>( o => { try { var url = string.format(routes.examples.list); ilist<exampleresponsemodel> exampleresponse = await executerequestasync<ilist<exampleresponsemodel>>(url, httpmethod.get); o.onnext(exampleresponse); o.oncompleted(); } catch (exception e) { o.onerror(e); } return disposable.empty; } ); }
is best practice? there more appropriate rx solution?
task<t>.toobservable()
you're looking for:
public iobservable<ilist<exampleresponsemodel>> listexamplesrx() { var url = string.format(routes.examples.list); return executerequestasync<ilist<exampleresponsemodel>>(url, httpmethod.get) .toobservable(); }
if want cold observable (ie. deferred execution) can wrap above in observable.defer
.
if need support cancellation, see how create iobservable task such unsubscribing cancels task?
wiki
Comments
Post a Comment