2014-10-28 55 views
0

虽然增加围绕Web客户端的任务foreach循环,它散发出以下错误添加foreach循环围绕Web客户端导致错误

foreach (string rssFeed in lstRSSFeeds) 
{ 
    // our web downloader 
    WebClient downloader = new WebClient(); 

    // our web address to download, notice the UriKind.Absolute 
    Uri uri = new Uri(rssFeed, UriKind.Absolute); 

    // we need to wait for the file to download completely, so lets hook the DownloadComplete Event 
    downloader.DownloadStringCompleted += new DownloadStringCompletedEventHandler(FileDownloadComplete); 

    // start the download 
    downloader.DownloadStringAsync(uri); 
} 

public void FileDownloadComplete(object sender, DownloadStringCompletedEventArgs e) 
{ 
    // e.Result will contain the files byte for byte 

    // your settings 
    XmlReaderSettings settings = new XmlReaderSettings(); 
    settings.DtdProcessing = DtdProcessing.Ignore; 

    // create a memory stream for us to use from the bytes of the downloaded file 
    MemoryStream ms = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(e.Result ?? "")); 

    // create your reader from the stream of bytes 
    XmlReader reader = XmlReader.Create(ms, settings); 
    SyndicationFeed feed = SyndicationFeed.Load(reader); 

    // do whatever you want with the reader 
    // ........ 
    reader.Close(); 
} 

导致错误:

A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.ni.dll

Additional information: Could not load file or assembly 'System.Windows.debug.resources, Version=2.0.6.0, Culture=en-US, PublicKeyToken=7cec85d7bea7798e' or one of its dependencies. The system cannot find the file specified.

回答

0

你要么需要坚持与当前的同步和基于事件的模型并使用DownloadString方法。

或(鼓!)欢迎的async await世界,让使用方法关键字await返回Task<T>

await downloader.DownloadStringAsync(uri); 

这里有一些方法来学习一下吧:

+0

它给这个错误,当我添加此代码:不能等待'空白'@abatishchev – user3795349 2014-10-28 16:57:55

+0

@ user3795349:当然!看到我更新的答案。 – abatishchev 2014-10-28 17:30:42