Using Retry with FromAsyncPattern
-
Thursday, July 21, 2011 4:49 PM
If I want to use Retry with FromAsyncPattern is there anything I need to be aware of?
e.g.
var tmp = Observable.FromAsyncPattern(request.BeginGetResponse, ar => this.Process(ar, request))(); tmp.Retry(2).Subscribe(result => {});
Trying to remember what I learned yesterday
All Replies
-
Thursday, July 21, 2011 8:49 PM
Hi,
Great question.
This won't work because FromAsyncPattern returns a function that returns a hot observable. The async method is only invoked once, when the function is invoked to get the observable. All calls to Subscribe on the hot observable share the same subscription side-effects. If the async method faults it will call OnError, which is then cached and replayed to subsequent subscribers.
Retry will observe the OnError and then call Subscribe again, only to observe the same error again immediately, until the retry count is exhausted.
The solution is to make the hot observable cold. You can do that using Defer as follows:
var requestAsync = Observable.FromAsyncPattern(request.BeginGetResponse, ar => this.Process(ar, request)); var tmp = Observable.Defer(requestAsync); tmp.Retry(2).Subscribe(result => {});
- Dave
http://davesexton.com/blog
- Proposed As Answer by Bart De Smet [MSFT]Owner Friday, July 22, 2011 12:51 AM
- Marked As Answer by Bart De Smet [MSFT]Owner Friday, November 11, 2011 9:20 PM
-
Thursday, July 21, 2011 10:08 PM
Dave,
Thanks, found a previous post of yours on this subject and integrated the use of Defer.
ta
Ollie
Trying to remember what I learned yesterday

