Reputation: 143
I'm trying to wrap my head around RX but somewhere my brain explodes :)
What I'm trying to do is invoke a WCF method in an async way through RX. Nothing special here, but when the WCF method throws an error I want to recreate the channel and give it another go (max 3 times).
What I have so far is:
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 around with Catch<TSource, TException>
, which allowed me to trap the exception and continue with the same observable, hence giving me what I wanted.
Only problem is that it runs forever, meaning if I keep on throwing exceptions the thing never stops!
Upvotes: 4
Views: 1380
Reputation: 117175
Try doing this:
var retryable = Observable.Defer(() => _observableFunc(666).Retry(3));
The Retry
extension method "Repeats the source observable sequence the specified number of times or until it successfully terminates."
Also, don't do this:
var _observable = _observableFunc(666);
var _defered = Observable.Defer(() => _observable);
There's no point deferring the observable after you've kicked it off.
You should do this instead:
var _defered = Observable.Defer(() => _observableFunc(666));
Then you're only one step away from my suggested solution at the top.
Upvotes: 3