C# 异步方法中的目标调用异常

C# 异步方法中的目标调用异常,c#,async-await,server-error,targetinvocationexception,C#,Async Await,Server Error,Targetinvocationexception,我试图从rss源中检索项目,但有时会出现TargetInvestment异常“无法连接到远程服务器”。我试图使用try-catch块来捕获这个错误,但我没有管理,因为我需要在其他代码中使用变量提要,就像这样,它是不可见的。有什么建议吗 public static async Task<List<FeedItem>> getFeedsAsync(string url) { //The web object that will retrieve our

我试图从rss源中检索项目,但有时会出现TargetInvestment异常“无法连接到远程服务器”。我试图使用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;
    }

 }
}
公共静态异步任务getFeedsAsync(字符串url)
{
//将检索我们的提要的web对象。。
SyndicationClient=新SyndicationClient();
//我们的源的URL。。
Uri feedUri=新的Uri(url);
//检索异步源。。
尝试
{
var feed=await client.RetrieveFeedAsync(feedUri);
}
捕获(目标)
{
}
//我们的订阅源列表。。
List feedData=新列表();
//用每个提要内容填充列表。。
foreach(feed.Items中的SyndicationItem项目)
{
FeedItem FeedItem=新的FeedItem();
feedItem.Content=item.Summary.Text;
feedItem.Link=item.Links[0].Uri;
feedItem.PubDate=item.PublishedDate.DateTime;
feedItem.Title=item.Title.Text;
尝试
{
feedItem.Author=item.Authors[0]。名称;
}
捕获(异常)
{ }
feedData.Add(feedItem);
}
返回反馈数据;
}
}
}
iasyncoperationwithprogressfeed;
//检索异步源。。
尝试
{
feed=wait client.RetrieveFeedAsync(feedUri);
}
捕获(目标)
{
}

这种错误是无法避免的。这是一个很好的例子

只有一种方法可以处理这些类型的错误:应用程序必须设计为预期错误并以合理的方式作出反应(例如,打开错误对话框或通知)


特别是,不要试图用空的<代码> catch 块来忽略它们。

如果我使用这个方法,我会从Windows .Web.CydioTy.SydioPotoFig中转换到Windows基础。IAsyncOperationWithProgress@TamaraCaligari您应该使用
RetrieveFeedAsync
返回的特定类型。似乎您有“使用”问题,所以请尝试使用完整的限定名称。如何显示catch块中的弹出对话框?您可以使用平台上可用的任何API。
IAsyncOperationWithProgress<SyndicationFeed, RetrievalProgress> feed;

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

}