簡體   English   中英

異步方法中的目標調用異常

[英]Target Invocation Exception in async method

我試圖從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;
    }

 }
}

無法避免這種錯誤。 這是一個外生的例外

處理這些類型的錯誤只有一種方法:您的應用程序必須設計成期望它們並以合理的方式做出反應(例如,顯示錯誤對話框或通知)。

特別是,不要試圖用空的catch塊忽略它們。

IAsyncOperationWithProgress<SyndicationFeed, RetrievalProgress> feed;

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

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM