0

I am trying to retrieve items from an rss feed but at times I get a TargetInvocationException 'unable to connect to remote server'. I am trying to use a try catch block to catch this error but I am not managing as I need the variable feed to be used throughout the other code and like this it is not visible. Any suggestions?

public static async Task<List<FeedItem>> getFeedsAsync(string url)
    {
       //The web object that will retrieve our feeds..
       SyndicationClient client = new SyndicationClient();

       //The URL of our feeds..
       Uri feedUri = new Uri(url);

       //Retrieve async the feeds..
       try
       {
           var feed = await client.RetrieveFeedAsync(feedUri);
       }
       catch (TargetInvocationException e)
       {

       }

       //The list of our feeds..
       List<FeedItem> feedData = new List<FeedItem>();

       //Fill up the list with each feed content..
       foreach (SyndicationItem item in feed.Items)
       {
             FeedItem feedItem = new FeedItem();
             feedItem.Content = item.Summary.Text;
             feedItem.Link = item.Links[0].Uri;
             feedItem.PubDate = item.PublishedDate.DateTime;
             feedItem.Title = item.Title.Text;

             try
             {
                 feedItem.Author = item.Authors[0].Name;
             }
             catch(ArgumentException)
             { }

             feedData.Add(feedItem);
         }

         return feedData;
    }

 }
}
svick
  • 236,525
  • 50
  • 385
  • 514
Tamara Caligari
  • 479
  • 3
  • 12
  • 28

2 Answers2

1

This kind of error cannot be prevented. It is an exogenous exception.

There is only one way to deal with those kinds of errors: your application must be designed to expect them and react in a reasonable way (e.g., bring up an error dialog or notification).

In particular, don't try to ignore them with an empty catch block.

Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810
0
IAsyncOperationWithProgress<SyndicationFeed, RetrievalProgress> feed;

//Retrieve async the feeds..
try
{
   feed = await client.RetrieveFeedAsync(feedUri);
}
catch (TargetInvocationException e)
{

}
i3arnon
  • 113,022
  • 33
  • 324
  • 344
  • If I use this I get an error that I cannot convert from Windows.Web.Syndication.SyndicationFeed to Windows.Foundation. IAsyncOperationWithProgress – Tamara Caligari Feb 02 '14 at 11:54
  • @TamaraCaligari You should use the specific type that `RetrieveFeedAsync` returns. It seems like you have a "using" problem, so try using the full qualified name. – i3arnon Feb 02 '14 at 13:11