我正在尝试从 rss 提要中检索项目,但有时我会收到 TargetInvocationException '无法连接到远程服务器'。我正在尝试使用 try catch 块来捕获此错误,但我没有进行管理,因为我需要在整个其他代码中使用变量提要,并且像这样它是不可见的。有什么建议么?
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;
}
}
}