【问题标题】:Unknown error using Digg API and URI handler (silverlight)使用 Digg API 和 URI 处理程序 (silverlight) 的未知错误
【发布时间】:2011-10-19 06:58:48
【问题描述】:

对于课程,我们必须按照教程创建一个 Silverlight 网站,该网站在 DIGG 中搜索给定主题。 (以本教程为基础:http://weblogs.asp.net/scottgu/archive/2010/02/22/first-look-at-silverlight-2.aspx

我们必须使用以下代码从 DIGG 获取信息。

    private void buttonSearch_Click(object sender, RoutedEventArgs e)
        {
            string topic = textboxSearchTopic.Text;

            WebClient digg = new WebClient();
            digg.DownloadStringCompleted +=
                              new DownloadStringCompletedEventHandler(digg_DownloadStringCompleted);
            digg.DownloadStringAsync(
                         new Uri("http://services.digg.com/1.0/story.getAll?count=10&topic="+topic)); 
}

void digg_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
            if (e.Error != null)
            {
                DisplayStories(e.Result);             
            }
}

private void DisplayStories(string xmlContent)
        {
            XDocument document = XDocument.Parse(xmlContent);

            var stories = from story in document.Descendants("story")
                          where story.Element("thumbnail")!=null
                          select new DiggStory
                         {
                             Id = (string)story.Attribute("id"),
                             Title = (string)story.Element("title"),
                             Description = (string)story.Element("description"),
                             ThumbNail = (string)story.Element("thumbnail").Attribute("src"),
                             HrefLink = (string)story.Attribute("link"),
                             NumDiggs = (int)story.Attribute("diggs")
                         };
         gridStories.ItemsSource = stories;
        }

当按下按钮搜索时,我们得到错误:

An exception occurred during the operation, making the result invalid.  Check InnerException for exception details.

   at System.ComponentModel.AsyncCompletedEventArgs.RaiseExceptionIfNecessary()
   at System.Net.OpenReadCompletedEventArgs.get_Result()
   at DiggSample.Views.Search.Digg_OpenReadCompleted(Object sender, OpenReadCompletedEventArgs e)
   at System.Net.WebClient.OnOpenReadCompleted(OpenReadCompletedEventArgs e)
   at System.Net.WebClient.OpenReadOperationCompleted(Object arg)

我已经知道 Digg API 已经过时了,但我认为这个错误与它没有任何关系。 (我们甚至得到了一个本地的 XML 文件,我们可以使用它但它仍然不起作用)

我不知道是什么原因造成的,我们也没有从老师那里得到太多帮助,所以我希望有人能帮助我们。

谢谢, 托马斯

【问题讨论】:

    标签: xml silverlight api uri digg


    【解决方案1】:

    对于这部分代码:

    if (e.Error != null)
    {
        DisplayStories(e.Result);             
    }
    

    如果 e.Error 为 not null,您是说显示故事。我认为您想将条件切换为e.Error == null,因为这意味着没有错误并且可以安全地使用结果。您可能希望在条件处设置断点以检查 e.Error 的值,以查看那里是否有异常。

    编辑:

    当您将条件更改为 e.Error == null 并且没有发生任何事情时,那是因为错误不为空,因此您的 DisplayStories(e.Result) 语句从未触发。

    有问题的例外SecurityException 发生是因为 Silverlight 浏览器内应用程序不允许您调用外部网站,除非该网站具有 Silverlight 跨域策略文件。不幸的是,Digg 的策略文件不再允许跨域访问,这意味着您将无法进行此调用,除非您以完全信任的方式在浏览器外运行您的应用程序。详情请见Network Security Access Restriction in Silverlight

    要将您的应用作为完全信任的浏览器外应用运行,请在 Visual Studio 中右键单击您的项目并选择属性。在“Silverlight”选项卡上,选中“启用浏览器耗尽”复选框。然后单击“超出浏览器设置”按钮。在对话框中,选中“在浏览器外运行时需要提升信任”框。在“调试”选项卡中,对于“开始操作”,选择“超出浏览器应用程序”并从下拉列表中选择您的项目。

    当你以这种方式运行时,你应该不再得到 SecurityException。

    【讨论】:

    • 如果我将其更改为 ==,它不会给我任何错误,也不会真正发生任何事情。我按下按钮,但没有任何反应。稍后在教程中它告诉我们使用以下代码:pastebin.com/CYywS9Wx 使用本地 XML 文件 (digglifestyle.xml),使用该代码我再次遇到相同的错误。感谢您的回复! :)
    • 非常感谢,它正在工作! (有点)看到 Digg API 已经过时并且不再工作,它并没有真正给出结果,但我找到了一个工作 XML 页面并且它工作!非常感谢
    • 但是没有办法解决这个问题吗?我想将其部署为网页,但如果我不能那么... :(
    • 除了让您的服务器(托管您的 silverlight 应用程序的服务器)包装 digg 服务并充当代理之外,没有办法解决这个问题。然后,您的 silverlight 应用程序可以向您的服务器而不是 digg 发出请求,并且不会出现安全异常。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多