Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#如何重新启动接收订阅_C#_Wpf_System.reactive - Fatal编程技术网

C#如何重新启动接收订阅

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);

假设我们有这个可观测序列(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);

就良好实践而言,这实际上取决于您的用例,以及如果webservice.Call抛出异常,您希望发生什么

为了解决您问题的具体部分,这里有一些很好的参考资料

如果可观察对象已完成-未完成或错误- 那么订阅已为您处理

这里介绍了各种错误处理技术,以及在您的场景中可能应用的技术

至于如何处理您的特定场景,有几个选项,这里只是一些想法

  • 您可以只处理webcall本身的异常,这样它就不会将流转换为OneError

  • 您也可以告诉它重试

  • AFAIK并没有具体的Rx最佳实践。如果发生异常,这实际上就是你想要发生什么。如果你根本不在乎,只是想让它继续存在,那么重试就可以了。如果您想捕获并记录日志,那么可以将捕获放在webservice调用上。如果您想检查异常,然后有不同的异常导致不同的输出,那么您可以在webservice调用上放置不同的捕获

    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);