C# 响应式扩展缓冲订阅
我是Rx的新手,很难找到解决问题的方法。我正在使用Rx通过客户端库开始下载。目前看来:C# 响应式扩展缓冲订阅,c#,buffer,system.reactive,C#,Buffer,System.reactive,我是Rx的新手,很难找到解决问题的方法。我正在使用Rx通过客户端库开始下载。目前看来: private void DownloadStuff(string descriptor, Action<Stuff> stuffAction) { this.stuffDownloader.GetStuffObservable(descriptor).Subscribe(x => stuffAction(x)) } private void DownloadStuff(字符串描述
private void DownloadStuff(string descriptor, Action<Stuff> stuffAction)
{
this.stuffDownloader.GetStuffObservable(descriptor).Subscribe(x => stuffAction(x))
}
private void DownloadStuff(字符串描述符,操作stufaction)
{
this.stuffDownloader.GetStuffObservable(描述符).Subscribe(x=>stuffAction(x))
}
其中,stuffDownloader是客户端库中定义的下载逻辑的包装器。但我遇到了一个问题,我调用DownloadStuff的次数太多,导致大量下载,并使系统无法正常工作。现在我想做的是
private void DownloadStuff(string descriptor, Action<Stuff> stuffAction)
{
this.stuffDownloader.GetStuffObservable(descriptor)
.SlowSubscribe(TimeSpan.FromMilliSeconds(50))
.Subscribe(x => stuffAction(x))
}
private void DownloadStuff(字符串描述符,操作stufaction)
{
this.stuffDownloader.GetStuffObservable(描述符)
.SlowSubscribe(时间跨度从毫秒(50))
.订阅(x=>stuffAction(x))
}
其中SlowSubscribe是仅在某个时间间隔上订阅的Rx操作的组合
通常我会把这些下载电话放在一个队列中,每隔一段时间就把它们取出来,但最近我一直在尝试通过Rx做更多的事情。我想到了三个解决方案:
我突然想到#2可以通过将一个IObservable描述符传递到客户端库,并以某种方式减缓描述符进入可观察对象的速度来实现。理论上,您可以使用Rx将请求视为事件。通过这种方式,您可以利用Rx的序列化特性来排队下载 我想你的网络层(或玩偶下载器)会为你做这件事,但如果你想加入我的黑客行列……这就是我想到的(Yeehaw!!) 一,。 不要通过一个动作,使用Rx!!您基本上失去了这里的错误处理,并为自己设置了奇怪的未处理异常
private void DownloadStuff(string descriptor, Action<Stuff> stuffAction)
现在你只需要在你的另一个方法中调用它
public IObservable<Stuff> GetStuffObservable(string descriptor, ISchedulerLongRunning scheduler)
{
return Observable.Create<Stuff>(o=>
{
try
{
var stuff = GetStuff(description);
o.OnNext(stuff);
o.OnCompleted();
}
catch(Exception ex)
{
o.OnError(ex);
}
return Disposable.Empty(); //If you want to be sync, you cant cancel!
})
.SubscribeOn(scheduler);
}
public IObservable GetStuffObservable(字符串描述符,isSchedulerLongRunning调度程序)
{
返回可观察的。创建(o=>
{
尝试
{
var stuff=GetStuff(描述);
o、 OnNext(stuff);
o、 未完成();
}
捕获(例外情况除外)
{
o、 OnError(ex);
}
return dispossible.Empty();//如果要同步,则不能取消!
})
.SubscribeOn(调度程序);
}
然而,在做了所有这些之后,我确信这不是你真正想要的。我希望系统中的其他地方会出现问题
另一种选择是考虑使用合并运算符和它的最大一致性特征?< /P>什么是你的实际问题?好的建议,但不是答案。问题是模糊的,但他们要求帮助,“我叫下载太多了,导致大量下载,并压倒了系统。”是的,用户似乎对答案不太感兴趣,也不想澄清他们的问题。
public IObservable<Stuff> GetStuffObservable(string descriptor, IScheduler scheduler)
private Stuff GetSync(string description)
{
var request = (HttpWebRequest)WebRequest.Create("http://se300328:90/");
var response =request.GetResponse();
var stuff = MapToStuff(response);
return stuff;
}
public IObservable<Stuff> GetStuffObservable(string descriptor, ISchedulerLongRunning scheduler)
{
return Observable.Create<Stuff>(o=>
{
try
{
var stuff = GetStuff(description);
o.OnNext(stuff);
o.OnCompleted();
}
catch(Exception ex)
{
o.OnError(ex);
}
return Disposable.Empty(); //If you want to be sync, you cant cancel!
})
.SubscribeOn(scheduler);
}