C#如何重新启动接收订阅
假设我们有这个可观测序列(RX)和匹配的订阅:C#如何重新启动接收订阅,c#,wpf,system.reactive,C#,Wpf,System.reactive,假设我们有这个可观测序列(RX)和匹配的订阅: var behaviorSubject = new BehaviorSubject<int>(3); var sequence = behaviorSubject.Select(x => this.webservice.Call(x)).Switch(); var subscription = this.sequence.Subscribe(this.Subject.OnNext, this.OnSequenceFaulted);
var behaviorSubject = new BehaviorSubject<int>(3);
var sequence = behaviorSubject.Select(x => this.webservice.Call(x)).Switch();
var subscription = this.sequence.Subscribe(this.Subject.OnNext, this.OnSequenceFaulted);
就良好实践而言,这实际上取决于您的用例,以及如果webservice.Call抛出异常,您希望发生什么 为了解决您问题的具体部分,这里有一些很好的参考资料 如果可观察对象已完成-未完成或错误- 那么订阅已为您处理 这里介绍了各种错误处理技术,以及在您的场景中可能应用的技术 至于如何处理您的特定场景,有几个选项,这里只是一些想法
public void OnSequenceFaulted(Exception e)
{
subscription?.Dispose();
subscription = sequence.Subscribe(this.Subject.OnNext, this.OnSequenceFaulted);
}
var sequence = behaviorSubject.Select(x => this.webservice.Call(x)
.Catch((Exception exc) =>
//do something with exception,
//maybe just return an empty observable and log the exception
).Switch();
var subscription = this.sequence.Retry().Subscribe(this.Subject.OnNext, this.OnSequenceFaulted);