仅当使用Rx + WebRequest + XDocument.Load进行修改时才检索XDocument [英] Retrieve XDocument only when modified using Rx + WebRequest + XDocument.Load
问题描述
我有以下两个观察值
System.Net.WebRequest req = System.Net.HttpWebRequest.Create("http://test.com/data.xml");
req.Method = "HEAD";
System.Net.WebRequest req = System.Net.HttpWebRequest.Create("http://test.com/data.xml");
req.Method = "HEAD";
var ob = Observable.FromAsyncPattern(req.BeginGetResponse, req.EndGetResponse);
var ob = Observable.FromAsyncPattern(req.BeginGetResponse, req.EndGetResponse);
ob().Select(x => x).Select(x => x.Headers["Last-Modified"]).DistinctUntilChanged(x => x);
ob().Select(x => x).Select(x => x.Headers["Last-Modified"]).DistinctUntilChanged(x => x);
Observable .Interval(TimeSpan.FromSeconds(1.0)) .Select(_ => XDocument.Load("http://test.com/data.xml"));
Observable .Interval(TimeSpan.FromSeconds(1.0)) .Select(_ => XDocument.Load("http://test.com/data.xml"));
我希望XDocument observable仅在最后修改的"标头大于先前请求的文档的任何想法时才执行?
I would like it that the XDocument observable is only executed when "last-modified" header is greater then the previously requested document any ideas?
推荐答案
首先.Select(x => x)是空操作,因此您可以删除它.
Firstly .Select(x=>x) is a no-op so you can remove that.
我会稍微修改一下代码.首先,让我们将其分解为各个组成部分:
I would change the code up a little bit. First lets break it down into its constituent parts:
1)计时器.每秒轮询一次服务器.
1) The Timer. Every second poll the server.
var poll = Observable.Interval(TimeSpan.FromSeconds(1));
var poll = Observable.Interval(TimeSpan.FromSeconds(1));
2)调用获取标头
var lastModified = Observable.FromAsyncPattern(req.BeginGetResponse,req.EndGetResponse).Select(x => x.Headers ["Last-Modified"]);
var lastModified = Observable.FromAsyncPattern(req.BeginGetResponse, req.EndGetResponse).Select(x => x.Headers["Last-Modified"]);
3)选择获取文档
.Select(_ => XDocument.Load("http://test.com/data.xml"));
.Select(_ => XDocument.Load("http://test.com/data.xml"));
我们应该能够很好地构成:
We should be able to compose that nicely:
var lastModified = from interval in Observable.Interval(TimeSpan.FromSeconds(1))
from response in Observable.FromAsyncPattern(req.BeginGetResponse, req.EndGetResponse)
select response.Headers["Last-Modified"];
var data = lastModified.DistinctUntilChanged().Select(_ => XDocument.Load("http://test.com/data.xml"));
data.Subscribe(dataXml=>
{
Console.WriteLine("Data has changed!");
Console.WriteLine(datXml);
});
Cavet我只是直接在浏览器中输入了.如果编译的话我会很棒.
这篇关于仅当使用Rx + WebRequest + XDocument.Load进行修改时才检索XDocument的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!