Observable.Retry doesn't work as expected

喜你入骨 提交于 2019-12-06 15:05:17

Rolling your own Retry is overkill in this case. You can achieve the same thing by simply wrapping your method call in a Defer block and it will be re-executed when the retry occurs.

var numbers = Enumerable.Range(1, 10).ToObservable();

var processed = numbers.SelectMany(n => 
  //Defer call passed method every time it is subscribed to,
  //Allowing the Retry to work correctly.
  Observable.Defer(() => 
    Process(n).ToObservable()).Retry()
);

processed.Subscribe( f => Console.WriteLine(f));
CharlesNRice

You are retrying back to the same Task that is in a faulted state. Retry will resubscribe back to the observable source. The source of your retry is the ToObservable(). It will not act like a task factory and make a new Task and since the task is faulted it continues to retry on the faulted task and will never be successful.

You can check out this answer how to make your own retry wrapper https://stackoverflow.com/a/6090049/1798889

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!