本文整理汇总了C#中ReplaySubject.OnError方法的典型用法代码示例。如果您正苦于以下问题:C# ReplaySubject.OnError方法的具体用法?C# ReplaySubject.OnError怎么用?C# ReplaySubject.OnError使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ReplaySubject
的用法示例。
在下文中一共展示了ReplaySubject.OnError方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateDownloadObservable
private static IObservable<byte[]> CreateDownloadObservable(Uri uri)
{
return Observable.Create<byte[]>(o => {
var result = new ReplaySubject<byte[]>();
var inner = Observable.Using(() => new WebClient(), wc => {
var obs = Observable
.FromEventPattern<
DownloadDataCompletedEventHandler,
DownloadDataCompletedEventArgs>(
h => wc.DownloadDataCompleted += h,
h => wc.DownloadDataCompleted -= h)
.Take(1);
wc.DownloadDataAsync(uri);
return obs;
}).Subscribe(ep => {
if (ep.EventArgs.Cancelled) {
result.OnCompleted();
} else {
if (ep.EventArgs.Error != null) {
result.OnError(ep.EventArgs.Error);
} else {
result.OnNext(ep.EventArgs.Result);
result.OnCompleted();
}
}
}, ex => {
result.OnError(ex);
});
return new CompositeDisposable(inner, result.Subscribe(o));
}).Retry(5);
}
示例2: errors_are_sent_through_scheduler
public void errors_are_sent_through_scheduler()
{
ManualScheduler scheduler = new ManualScheduler();
ReplaySubject<int> subject = new ReplaySubject<int>(scheduler);
StatsObserver<int> stats = new StatsObserver<int>();
subject.OnError(new Exception());
subject.Subscribe(stats);
Assert.IsFalse(stats.ErrorCalled);
scheduler.RunNext();
Assert.IsTrue(stats.ErrorCalled);
}
示例3: buffer_size_includes_onerror
public void buffer_size_includes_onerror()
{
ReplaySubject<int> subject = new ReplaySubject<int>(2);
StatsObserver<int> stats = new StatsObserver<int>();
subject.OnNext(1);
subject.OnNext(2);
subject.OnNext(3);
subject.OnError(new Exception());
subject.Subscribe(stats);
Assert.AreEqual(1, stats.NextCount);
Assert.IsTrue(stats.NextValues.SequenceEqual(new int[] { 3 }));
Assert.IsTrue(stats.ErrorCalled);
}
示例4: CallOperationOnService
private static IObservable<ServiceState> CallOperationOnService(ServiceBase serviceBase, Operation operation)
{
ReplaySubject<ServiceState> result = new ReplaySubject<ServiceState>();
result.OnNext(operation.InitialState);
var methodObs = Observable.Start(() =>
{
Type serviceBaseType = serviceBase.GetType();
object[] parameters = null;
if (operation == Operations.Start)
{
parameters = new object[] { null };
}
try
{
serviceBaseType.InvokeMember(operation.MethodCall, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.InvokeMethod, null, serviceBase, parameters);
}
catch (Exception ex)
{
throw new Exception(string.Format("An exception was thrown while trying to call the {0} of the {1} service. Examine the inner exception for more information.", operation, serviceBase.ServiceName), ex.InnerException);
}
});
methodObs.Subscribe
(
_ =>
{
result.OnNext(operation.FinishedState);
result.OnCompleted();
},
ex =>
{
result.OnNext(operation.ErrorState);
result.OnError(ex);
}
);
return result;
}
示例5: Errored_to_late_subscriber_ReplayByTime
public void Errored_to_late_subscriber_ReplayByTime()
{
var expectedException = new Exception("Test");
var s = new ReplaySubject<int>(TimeSpan.FromMinutes(1));
s.OnNext(1);
s.OnNext(2);
s.OnNext(3);
s.OnError(expectedException);
var scheduler = new TestScheduler();
var observer = scheduler.CreateObserver<int>();
s.Subscribe(observer);
Assert.Equal(4, observer.Messages.Count);
Assert.Equal(1, observer.Messages[0].Value.Value);
Assert.Equal(2, observer.Messages[1].Value.Value);
Assert.Equal(3, observer.Messages[2].Value.Value);
Assert.Equal(NotificationKind.OnError, observer.Messages[3].Value.Kind);
Assert.Equal(expectedException, observer.Messages[3].Value.Exception);
}
示例6: HasObservers_OnError
private static void HasObservers_OnError(ReplaySubject<int> s)
{
Assert.False(s.HasObservers);
var d = s.Subscribe(_ => { }, ex => { });
Assert.True(s.HasObservers);
s.OnNext(42);
Assert.True(s.HasObservers);
s.OnError(new Exception());
Assert.False(s.HasObservers);
}
示例7: ReplaySubject
public void ReplaySubject()
{
// OnCompletedPattern
{
var subject = new ReplaySubject<int>();
var onNext = new List<int>();
var exception = new List<Exception>();
int onCompletedCallCount = 0;
var _ = subject.Subscribe(x => onNext.Add(x), x => exception.Add(x), () => onCompletedCallCount++);
subject.OnNext(1);
subject.OnNext(10);
subject.OnNext(100);
subject.OnNext(1000);
onNext.IsCollection(1, 10, 100, 1000);
// replay subscription
onNext.Clear();
_.Dispose();
subject.Subscribe(x => onNext.Add(x), x => exception.Add(x), () => onCompletedCallCount++);
onNext.IsCollection(1, 10, 100, 1000);
subject.OnCompleted();
onCompletedCallCount.Is(1);
subject.OnNext(1);
subject.OnNext(10);
subject.OnNext(100);
onNext.Count.Is(4);
subject.OnCompleted();
subject.OnError(new Exception());
exception.Count.Is(0);
onCompletedCallCount.Is(1);
// ++subscription
onNext.Clear();
onCompletedCallCount = 0;
subject.Subscribe(x => onNext.Add(x), x => exception.Add(x), () => onCompletedCallCount++);
onNext.IsCollection(1, 10, 100, 1000);
exception.Count.Is(0);
onCompletedCallCount.Is(1);
}
// OnErrorPattern
{
var subject = new ReplaySubject<int>();
var onNext = new List<int>();
var exception = new List<Exception>();
int onCompletedCallCount = 0;
subject.Subscribe(x => onNext.Add(x), x => exception.Add(x), () => onCompletedCallCount++);
subject.OnNext(1);
subject.OnNext(10);
subject.OnNext(100);
subject.OnNext(1000);
onNext.IsCollection(1, 10, 100, 1000);
subject.OnError(new Exception());
exception.Count.Is(1);
subject.OnNext(1);
subject.OnNext(10);
subject.OnNext(100);
onNext.Count.Is(4);
subject.OnCompleted();
subject.OnError(new Exception());
exception.Count.Is(1);
onCompletedCallCount.Is(0);
// ++subscription
onNext.Clear();
exception.Clear();
onCompletedCallCount = 0;
subject.Subscribe(x => onNext.Add(x), x => exception.Add(x), () => onCompletedCallCount++);
onNext.IsCollection(1, 10, 100, 1000);
exception.Count.Is(1);
onCompletedCallCount.Is(0);
}
}
示例8: QueueBackgroundDownloads
public IObservable<int> QueueBackgroundDownloads(IEnumerable<string> urls, IEnumerable<string> localPaths)
{
var ret = new ReplaySubject<int>();
var jobFiles = urls.Zip(localPaths, (url, path) => new {url, path});
var job = manager.CreateJob(applicationName + "_" + Guid.NewGuid() , JobType.Download);
job.OnJobError += (o, e) => ret.OnError(new BitsException(e.Error));
job.OnJobTransferred += (o, e) => {
try {
job.Complete();
if (job.ErrorCount != 0) {
ret.OnError(new BitsException(job.Error));
} else {
ret.OnNext(100);
ret.OnCompleted();
}
} catch (Exception ex) {
ret.OnError(ex);
}
};
jobFiles
.Where(x => !File.Exists(x.path))
.ForEach(x => job.AddFile(x.url, x.path));
job.Resume();
return ret;
}
示例9: HasObservers_OnError
public void HasObservers_OnError()
{
var s = new ReplaySubject<int>();
Assert.IsFalse(s.HasObservers);
var d = s.Subscribe(_ => { }, ex => { });
Assert.IsTrue(s.HasObservers);
s.OnNext(42);
Assert.IsTrue(s.HasObservers);
s.OnError(new Exception());
Assert.IsFalse(s.HasObservers);
}
示例10: window_includes_onerror
public void window_includes_onerror()
{
ManualScheduler scheduler = new ManualScheduler();
ReplaySubject<int> subject =
new ReplaySubject<int>(TimeSpan.FromMilliseconds(10), scheduler);
StatsObserver<int> stats = new StatsObserver<int>();
DateTime start = DateTime.UtcNow;
scheduler.Now = start;
subject.OnNext(1);
scheduler.Now = start.AddMilliseconds(5);
subject.OnNext(2);
scheduler.Now = start.AddMilliseconds(10);
subject.OnError(new Exception());
scheduler.Now = start.AddMilliseconds(11);
subject.Subscribe(stats);
scheduler.RunAll();
Assert.AreEqual(1, stats.NextCount);
Assert.IsTrue(stats.NextValues.SequenceEqual(new int[] { 2 }));
Assert.IsTrue(stats.ErrorCalled);
}
示例11: window_can_pass_error
public void window_can_pass_error()
{
ManualScheduler scheduler = new ManualScheduler();
ReplaySubject<int> subject =
new ReplaySubject<int>(TimeSpan.FromMilliseconds(10), scheduler);
StatsObserver<int> stats = new StatsObserver<int>();
DateTime start = DateTime.UtcNow;
scheduler.Now = start;
subject.OnError(new Exception());
scheduler.Now = start.AddMilliseconds(11);
subject.Subscribe(stats);
scheduler.RunAll();
Assert.IsFalse(stats.ErrorCalled);
}
示例12: replays_values_when_errored
public void replays_values_when_errored()
{
ReplaySubject<int> subject = new ReplaySubject<int>();
StatsObserver<int> stats = new StatsObserver<int>();
subject.OnNext(1);
subject.OnNext(2);
subject.OnNext(3);
subject.OnError(new Exception());
subject.Subscribe(stats);
Assert.AreEqual(3, stats.NextCount);
Assert.IsTrue(stats.NextValues.SequenceEqual(new int[] { 1, 2, 3 }));
Assert.IsTrue(stats.ErrorCalled);
}
示例13: replays_error_sequence
public void replays_error_sequence()
{
ReplaySubject<int> subject = new ReplaySubject<int>();
StatsObserver<int> stats = new StatsObserver<int>();
subject.OnError(new Exception());
subject.Subscribe(stats);
Assert.IsTrue(stats.ErrorCalled);
}
示例14: DoAsyncSend
private IObservable<string> DoAsyncSend(string cmd)
{
if (this.scheduler == null)
throw new Exception("Connection closed.");
var subject = new ReplaySubject<string>();
this.scheduler.Schedule(() =>
{
try
{
if (!this.Connect())
return;
var writer = new StreamWriter(client.GetStream()) { AutoFlush = true };
var reader = new StreamReader(client.GetStream());
writer.WriteLine(cmd);
var response = ReadLine(reader);
subject.OnNext(response);
if (Regex.IsMatch(response, "201.*"))
subject.OnNext(ReadLine(reader));
else if (Regex.IsMatch(response, "200.*"))
{
while (response != string.Empty)
{
response = ReadLine(reader);
subject.OnNext(response);
}
}
else if (Regex.IsMatch(response, "400.*"))
throw new Exception("Command not understood.");
else if (Regex.IsMatch(response, "401.*"))
throw new Exception("Illegal Command.");
else if (Regex.IsMatch(response, "402.*"))
throw new Exception("Parameter missing.");
else if (Regex.IsMatch(response, "403.*"))
throw new Exception("Illegal parameter.");
else if (Regex.IsMatch(response, "404.*"))
throw new Exception("Media file not found.");
else if (Regex.IsMatch(response, "500.*"))
throw new Exception("Internal server error.");
else if (Regex.IsMatch(response, "501.*"))
throw new Exception("Internal server error.");
else if (Regex.IsMatch(response, "502.*"))
throw new Exception("Media file unreadable.");
}
catch (IOException ex)
{
this.Reset();
subject.OnError(ex);
}
catch(ObjectDisposedException ex)
{
this.Reset();
subject.OnError(ex);
}
catch (Exception ex)
{
subject.OnError(ex);
}
subject.OnCompleted();
});
return subject;
}