4

Can we read RSS by async/await?

XmlReader reader = XmlReader.Create("http://localhost/feeds/serializedFeed.xml");
SyndicationFeed feed = SyndicationFeed.Load(reader);

Any clue?

NoWar
  • 36,338
  • 80
  • 323
  • 498
  • 1
    Do you mean something like [this](http://stackoverflow.com/questions/14072639/reading-syndicationfeed-in-threadpool-runasync)? – diiN__________ May 18 '16 at 13:53
  • @diiN_ Yeah would you mind to provide an answer? The problem is SyndicationClient is available from Minimum supported client Windows 8 [Windows Store apps, desktop apps] https://msdn.microsoft.com/en-us/library/windows/apps/windows.web.syndication.syndicationclient.aspx – NoWar May 18 '16 at 14:01

2 Answers2

3

Your solution is not using async/await, has too much code, and I suspect your approach is prone to deadlocks under certain scenarios.

Simply do this

var reader = XmlReader.Create("http://localhost/feeds/serializedFeed.xml");
var feed = await Task.Run(() => SyndicationFeed.Load(reader));
Etienne Charland
  • 3,424
  • 5
  • 28
  • 58
  • I noticed that XmlReader has an async option: `XmlReader reader = XmlReader.Create(feedUrl, settings: new XmlReaderSettings { Async = true });` Does that help or hurt matters? – MikeT Aug 14 '19 at 23:31
0

Ok folk, here is working solution

private  void   GetRSS(string rssUrl)
        {
            Task.Factory.StartNew(() => {
                using (XmlReader r = XmlReader.Create(rssUrl))
                {
                    SyndicationFeed feed = SyndicationFeed.Load(r);
                    Action bindData = () => {
                        lstFeedItems.ItemsSource = feed.Items;
                    };
                    this.Dispatcher.InvokeAsync(bindData);
                }
            });
        }
NoWar
  • 36,338
  • 80
  • 323
  • 498