RX, try again and allow exception handling

I'm trying to wrap my head around the RX, but somewhere my brain explodes :)

What I'm trying to do is call the WCF method async via RX. Nothing special here, but when the WCF method throws an error, I want to recreate the channel and give it another move (maximum 3 times).

What I still have:

var _sc = new Service.Service1Client(); var _observableFunc = Observable.FromAsyncPattern<int, string>(_sc.BeginGetData, _sc.EndGetData); var _observable = _observableFunc(666); var _defered = Observable.Defer(() => _observable); // Here something should be done, but don't know what... using (_retryable.Subscribe(x => Console.WriteLine("Async ==> '{0}'", x), ex => Console.WriteLine("Oops ==> {0}", ex.Message))) { Console.ReadLine(); } 

I played with Catch<TSource, TException> , which allowed me to Catch<TSource, TException> exception and continue with the same observable, therefore giving me what I wanted. The only problem is that it works forever, that is, if I keep throwing exceptions, the thing never stops!

+4
source share
1 answer

Try to do this:

 var retryable = Observable.Defer(() => _observableFunc(666).Retry(3)); 

Retry Extension Method "Repeats the observed source sequence a specified number of times or until it succeeds."

Also, do not do this:

 var _observable = _observableFunc(666); var _defered = Observable.Defer(() => _observable); 

It makes no sense to postpone the observable after you let it go.

Instead, you should do this:

 var _defered = Observable.Defer(() => _observableFunc(666)); 

Then you are only one step away from my proposed solution at the top.

+1
source

Source: https://habr.com/ru/post/1434824/


All Articles