如何将IObservable
流中的异常转换为普通域对象并以透明方式重新订阅流?
附录:正如James在评论中指出的那样,我的用例理念就像是在一个不可靠的来源上有一个应该是连续的流,例如一个网络。如果出现故障,只需尝试重新连接到源,但通知下游处理器。
事实上,这与我在Translating a piece of asynchronous C# code to F# (with Reactive Extensions and FSharpx)的另一个问题有关,后者又来自How to implement polling using Observables?。
事实上,现在我想到了,我可以先在How to write a generic, recursive extension method in F#?(“RetryAfterDelay”)上使用代码(使用更多参数来调整RetryAfterDelay
行为)和链它与此实现。重试耗尽时,将产生域错误并重新启动轮询器。当然,可能会有一种更有效的方式,但尽管如此。 :) ...或者只提供一个回调函数来记录错误而不是将它们转换为域事件,好吧,选择比比皆是......
但回到原始代码......
例如,如果我有
public enum EventTypeEnum
{
None = 0,
Normal = 1,
Faulted = 2
}
public class Event
{
public EventTypeEnum Type { get; set; }
}
private static IObservable<int> FaultingSequence1()
{
var subject = new ReplaySubject<int>();
subject.OnNext(1);
subject.OnNext(2);
subject.OnError(new InvalidOperationException("Something went wrong!"));
return subject;
}
private static IEnumerable<int> FaultingSequence2()
{
for(int i = 0; i < 3; ++i)
{
yield return 1;
}
throw new InvalidOperationException("Something went wrong!");
}
//Additional pondering: Why isn't FaultingSequence2().ToObservable() too be procted by Catch?
//
//This part is for illustratory purposes here. This is the piece I'd like
//behave so that exceptions would get transformed to Events with EventTypeEnum.Faulted
//and passed along to the stream that's been subscribed to while resubscribing to
//FaultingSequence1. That is, the subscribed would learn about the fault through a
//domain event type.
//Retry does the resubscribing, but only on OnError.
var stream = FaultingSequence1().Catch<int, Exception>(ex =>
{
Console.WriteLine("Exception: {0}", ex);
return Observable.Throw<int>(ex);
}).Retry().Select(i => new Event { Type = EventTypeEnum.Normal });
//How to get this to print "Event type: Normal", "Event type: Normal", "Event type: Faulted"?
stream.Subscribe(i => Console.WriteLine("Event type: {0}", i.Type));
这个问题现在真的让我受益匪浅!有什么建议吗?
答案 0 :(得分:4)
有一个名为Materialize
的运算符,它将每个事件转换为Notification<T>
:
OnNext:
OnNext a Notification<T> with Kind OnNext containing a value.
OnError:
OnNext a Notification<T> with Kind OnError containing an exception.
OnCompleted.
OnCompleted:
OnNext a Notification<T> with Kind OnCompleted
OnCompleted.
因此,当调用OnError或OnCompleted时,订阅仍然完成,但永远不会在订阅服务器上调用OnError。所以你可以做这样的事......
source
.Materialize()
.Repeat();
但是,即使原始订阅自然完成(通过OnCompleted),也会重新订阅源。
所以也许你仍然希望调用OnError,但是你也希望原始OnError中的异常通过Notification<T>
内的OnNext传递。为此,你可以使用这样的东西:
source
.Materialize()
.SelectMany(notification =>
notification.Kind == NotificationKind.OnError
? Observable.Return(notification).Concat(Observable.Exception(notification.Exception))
: Observable.Return(notification)
)
.Retry();
通过这种方式,如果订阅自然完成(通过OnCompleted),则不会重新订阅源。
完成设置之后,每个设置都足以将每种类型的通知映射到您要使用的任何域对象:
source
.Materialize()
.SelectMany(notification =>
notification.Kind == NotificationKind.OnError
? Observable.Return(notification).Concat(Observable.Exception(notification.Exception))
: Observable.Return(notification)
)
.Retry()
.Map(notification => {
switch (notification.Kind) {
case (NotificationKind.OnNext): return // something.
case (NotificationKind.OnError): return // something.
case (NotificationKind.OnCompleted): return // something.
default: throw new NotImplementedException();
}
});